Author Topic: Can we do word splitting of a text variable?  (Read 1355 times)

DopeGhoti

  • Newbie
  • *
  • Posts: 1
Can we do word splitting of a text variable?
« on: December 28, 2019, 11:19:51 AM »
If I have a text variable, 'words' containing the text 'apple banana', is it possible to extract the two words into new variables?

Context:  I have a variable I'm already using that sets a grid location to a row and column, for instance 'B 13', for later use.  A new use-case has arisen whereby I'd like to see the row and column individually, and I did not have the foresight to split them originally.  Rather than have to edit everything stem to stern, I thought about extracting 'row' and 'column' from 'grid'.  Is this doable?

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Can we do word splitting of a text variable?
« Reply #1 on: December 29, 2019, 12:25:44 AM »
This can be done using "{TXTSUBSTR:}" and "{TXTPOS:}" tokens:
Code: [Select]
Set Text [grid] to 'B 13'
Set Text [row] to '{TXTSUBSTR:grid:0:{TXTPOS:" ":grid:0}}'
Set Text [column] to '{TXTSUBSTR:grid:{EXP:{TXTPOS:" ":grid:0}+1}:}'
Write [Blue] 'Row = '{TXT:row}', Column = '{TXT:column}'' to log


Though, it can also be done with an inline function using string.Split(), which would be easier to extend later if required:
Code: [Select]
Set Text [grid] to 'B 13'
Inline C# Function: Bisect string
Write [Blue] 'Row = '{TXT:row}', Column = '{TXT:column}'' to log

Where "Bisect string" contains:
Code: [Select]
public class VAInline
{
public void main()
{
string[] txtSegments = VA.GetText("grid").Split(' ');
VA.SetText("row", txtSegments[0]);
VA.SetText("column", txtSegments[1]);
}
}

frankly

  • Newbie
  • *
  • Posts: 17
Re: Can we do word splitting of a text variable?
« Reply #2 on: July 05, 2021, 07:47:44 AM »
Does this work to split XY to X and Y

nvm: Pfeil's stuff made it so easy to understand. AGAIN. stop this wizardry!