3

In TI-BASIC, the + operation is overloaded for string concatenation (in this, if nothing else, TI-BASIC joins the rest of the world).

However, any attempt to concatenate involving an empty string raises a Dimension Mismatch error:

"Fizz"+"Buzz"
        FizzBuzz 
"Fizz"+""
           Error
""+"Buzz"
           Error
""+""
           Error

Why does this occur, and is there an elegant workaround? I've been using a starting space and truncating the string when necessary (doesn't always work well) or using a loop to add characters one at a time (slow).

  • why would you need to concatenate an empty string? Just curious – Meepo Dec 12 '17 at 02:57
  • @Meepo For example, when writing the FizzBuzz program, I may want to keep a string each iteration and append "Fizz" if the current number is divisible by 3 and "Buzz" if by 5. This covers three of the four cases. Also, I may want to use an unknown string in a program—what if it's empty? – Khuldraeseth na'Barya Dec 12 '17 at 03:40
  • I think ti-basic deals with empty strings in a weird way (probably to save memory), so if you post some more code I would be happy to help you find a way around this – Meepo Dec 12 '17 at 03:55

2 Answers2

2

The best way depends on what you are doing.

If you have a string (in this case, Str1) that you need to concatenate with another (Str2), and you don't know if it is empty, then this is a good general-case solution:

Str2
If length(Str1
Str1+Str2

If you need to loop and add a stuff to the string each time, then this is your best solution:

Before the loop:

" →Str1

In the loop:

Str1+<stuff_that_isn't_an_empty_string>→Str1

After the loop:

sub(Str1,2,length(Str1)-1→Str1

There are other situations, too, and if you have a specific situation, then you should post a simplified version of the relevant code.

Hope this helps!

iPhoenix
  • 719
  • 7
  • 20
0

It is very unfortunate that TI-Basic doesn't support empty strings. If you are starting with an empty string and adding chars, you have to do something like this:

"?
For(I,1,3
Prompt Str1
Ans+Str1
End
sub(Ans,2,length(Ans)-1

Another useful trick is that if you have a string that you are eventually going to evaluate using expr(, you can do "("+Str1+")"→Str1 and then freely do search and replace on the string. This is a necessary workaround since you can't search and replace any text involving the first or last character in a string.

Timtech
  • 1,224
  • 2
  • 19
  • 30