Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

String handling


buck Aug 19, 2009 06:20 PM

I have a question about copying a string from one location to another. Suppose I have:

Public CurrentStr(23) As String * 150
Public TempData(23) As String * 150

I want to copy the strings from TempData() to CurrentStr()if the first character of the first TempData() string is an ascii 'S'.

Should this work?

if TempData(1,1) = 83 Then
dx = 1
Do
CurrentStr(dx) = TempData(dx)
dx = dx + 1
Loop Until dx = 22
EndIf

Thanks, Buck


Dana Aug 20, 2009 04:51 PM

if TempData(1,1) = 83 Then

Hello Buck

I would expect your code to work with just a couple of changes:

if TempData(1,1,1) = CHR(83) Then

It is my understanding (which can be questionable!) that specific characters into a string are referenced using the third dimension (strings can be declared no larger than two dimensions; e.g., String1(x,y), with z reserved for character reference). Thus, with the modified code I would expect that any time the first character was "S", the data would be copied.

What I'm seeing in my testing is that regardless of whether I use TempData(1) (1,1) or (1,1,1), if the string is "S" the values are copied. However, if the string is "SSS" or "Sing" or anything other than "S", it doesn't work. I would have expected any string starting with a capital "S" to work.

I will look into this further. In the meantime, using:

If Mid(TempData(1),1,1) = CHR(83) Then

seems to work -- any time the first character is "S", the string is copied.

Regards,

Dana W.


Dana Aug 20, 2009 06:28 PM

My understanding of StringVar(1,1,1) was incorrect :)

Accessing the string using the 3rd dimension allows you to get to the *remainder* of the string that starts at the 3rd dimension specified. If TempData is "STOP", then TempData(1,1,2) is "TOP", TempData(1,1,3) is "OP", TempData(1,1,1) is "STOP".

Our developer also pointed out a couple of other things.

In a recent OS, we added the ability to set Strings to a size of 1 (normally, strings default to 16 bytes if Size is not used -- 15 with 1 terminating character). This allows more efficient storage of a single character by using only 4 bytes (variable memory has to be allocated in 4-byte chunks). Setting Size to 1 would allow you to access only the first character using the Var(1,1,1) syntax. If STOP were the variable, Var(1,1,1) would return S, Var(1,1,2) would return T, etc.

He also pointed out that "S" can be used in place of CHR(83) :)

I hope all this helps!
Dana


IslandMan Aug 21, 2009 09:53 AM

Since we're looking for the 1st character in the string, why wouldn't "Left" be a good choice for this?

IslandMan


buck Aug 21, 2009 04:38 PM

I was indeed able to get it to work using the Left() instruction. I used it to get the 1st two characters of the string and compared it to "S," which are the expected characters.

Thanks for the suggestion.


Buck


Dana Aug 21, 2009 05:38 PM

Since we're looking for the 1st character in the string, why wouldn't "Left" be a good choice for this?

Because I didn't think of that one ?? :)

(there are usually at least three ways to do what you want to do in the dataloggers!)

Someone else here at CSI said his first thought was to use InStr.

Dana


buck Aug 26, 2009 05:07 PM

What is the best way to clear a string? That is, make every character of that string be set to the null character CHR(0)?

Log in or register to post/reply in the forum.