[Gambas-user] Catch NULL character

Doriano Blengino doriano.blengino at ...1909...
Sat Mar 28 08:18:16 CET 2009


CelticBhoy ha scritto:
> What is wrong with this line :-
>
> IF sElement[iLoop] = NULL THEN sElement[iLoop] = "0"
>
> I want to catch a null character and change it to "0".
>   
You are using an array notation, ie, sElement[] designes an array of 
strings, each composed of many characters.
Moreover, NULL is not the same as chr$(0), but refers to the null 
pointer (or an empty string), but this is different from C.

if sElement is a simple string, and assuming iLoop goes from 1 to the 
length of the string, then you test like this:

    if mid$(sElement, iLoop, 1)=chr$(0) then sElement=left$(1,iLoop-1) & 
"0" & mid$(sElement, iLoop+1)

Note two things here: gambas does not have indexing of strings, like C. 
Use left$(), mid$() and so on is the way to analyze strings. This also 
means that you can not change a single char in a string, but you have to 
reconstruct the string itself.
Anyway, there are more powerful instruction - see the docs about string 
manipulation; a single replace() could do what you want:

    sElement = Replace$(sElement, chr$(0), "0")


If is it true that sElement should be an array of strings, then perhaps 
you are not looking for chr$(0), or you need two indexes, or you should 
provide more information about what you want to do...

Cheers,

-- 
Doriano Blengino

"Listen twice before you speak.
This is why we have two ears, but only one mouth."





More information about the User mailing list