Splitting a string on "\n"

I have used GetText on an area of screen text and it has returned values separated by “\n”. This is perfectly fine as there are line-breaks in the text and actually is exactly what I wanted, however, I want to convert the string into a list, splitting on the “\n”.

My issue is that I get the following error from this line of code:

put text split by "\\n" into itemList

Sensetalk compiler exception: syntax error - cant understand “n” at line…

I initially thought there was a delimiting issue, having originally tried to split on “\n” so I switched to “\n” but the same error occurs…

How can you use the split function when there are escape sequences in the string?

many thanks

I’m not sure of your exact context, but keep in mind that SenseTalk normally treats strings as literal, unless the string is preceded by @ like @"\n".
For what you’re trying to do, I would first try put text split by return into itemList – the return constant is a newline character.

1 Like

Hi Doug

Thanks for the suggestion. I tried this but no split seems to occur - the original string simply gets put in the target variable unchanged… at least untill i found another post on here about querying item count:

put the number of items in myList

I added this into my script to log what the result was and all of a suddenly the list was acting like a list, almost as if running that query made the variable realise what it was supposed to be?

Seems to be working now, so thank you, just not sure if it is safe to remove that query again :smiley:

I think you’re getting confused about what is happening. Using split will always result in a list, even if the delimiter you’re splitting by doesn’t occur in the string as shown here:

put "Hello" split by "x" into foo
put foo --> ["Hello"]

Asking for the number of items in something doesn’t change the value at all, it just counts the items. Unfortunately, this can be confusing too, because item has a dual meaning – it can be either list items or text items. To be sure you are counting list items you have to be explicit and ask for the number of list items.

I can’t be sure about exactly what is happening in your script without seeing your actual code.