Is there a good way I can split a string containing delimiters in TI-basic? I know about the Sub( command, but I'm not sure if that would help in my case.

Say I have Str1 and it contains "63621}Test}ThisIsSomeText}1.2.3}". My goal is to get them split by their delimiter "}" so that way I maybe can do something like
Output(part1)
Output(part2)
Output(part3)
Output(part4)

and get something like this:
63621
Test
ThisIsSomeText
1.2.3

The length of each part will be different every time and there will not always be just 4 items
This works on my TI 84+. The -> is the Sto key. The “ “->Str3 moves a space to the Str3 string, but I don’t know how to initialize Str3 without the space. This displays the text below with a space before each of the lines.

63621
TEST
THISISSOMETEXT
1.2.3

I code on an iPad, so I’m not very good on the TI 84.


Code:
"63621}TEST}THISISSOMETEXT}1.2.3}"->Str1
" "->Str3
For(S,1,length(Str1)
sub(Str1,S,1)->Str2
If Str2="}"
Then
Disp Str3
" "->Str3
Else
Str3+Str2->Str3
End
End



Code:

This is the code on my iPad.
I wrote it there first and converted it to TI basic above.

function setup()
    str1="63621}TEST}THISISSOMETEXT}1.2.3}"
    str3=""
    for s=1,#str1 do
        str2=string.sub(str1,s,s)
        if str2=="}" then
            print(str3)
            str3=""
        else
            str3=str3..str2
        end
    end
end
Matt11 wrote:
Is there a good way I can split a string containing delimiters in TI-basic? I know about the Sub( command, but I'm not sure if that would help in my case.

Yep, that is totally doable, and the Sub() command is definitely helpful!
A lot of languages have a built-in split() methods, which would return a collection object, but since ti-basic isn't object-oriented and by extension, doesn't support collection types, there's no data type that could contain the result of a traditional split() command. However, that doesn't prevent us from simply going through the string and looking for the delimiters.

Here's how I would go about doing it:

Code:
"LINE{DELIMITER{TEST{STRING→Str1
1→B
inString(Str1,"{→A
While A
If A>1
Disp sub(Str1,B,A-B
A+1→B
inString(Str1,"{",B→A
End
If B<length(Str1
Disp sub(Str1,B,length(Str1)-B+1


This routine will account for the input string starting or ending with a line delimiter, as well as the input string containing no delimiters at all. If you already know that the string won't start or end with a delimiter, you can take out the two if statements. It won't account for there being 2 delimiters in a row though. If you want to use an extra delimiter to signify it should skip 2 lines, you can always use a space ("{ {") so that there aren't 2 delimiters in a row.
Here’s a modification of my above code.


Code:

"63621}TEST}THISISSOMETEXT}1.2.3}"→Str1
1→B
For(S,1,length(Str1)
If sub(Str1,S,1)="}"
Then
Disp sub(Str1,B,S-B)
S+1→B
End
End
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement