Bug in substring2 function ?

DOM85

Active Member
Licensed User
Longtime User
Hi,

Substring2 function needs 2 parameters.
- The first one is the beginning position in the source string(from 0),
- And the second one would have to be the ending position (from 0). But in fact it is not, we have to add 1 to this position.

Why the ending position is not the true ending position, synthax would be better. Is there any useful reason for that?

Thank you for your answer.
 

klaus

Expert
Licensed User
Longtime User
If you look at the help file:
'Returns a new string which is a substring of the original string.
The new string will include the character at BeginIndex and will extend to the character at EndIndex, not including the last character.'

I agree with you that it is confusing.

Best regards.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Substring2 function needs 2 parameters.
- The first one is the beginning position in the source string(from 0),
- And the second one would have to be the ending position (from 0). But in fact it is not, we have to add 1 to this position.

Why the ending position is not the true ending position, synthax would be better. Is there any useful reason for that?

In the "VB6 to B4A" thread, neotechni added this post with code for subs which use the same syntax as VB6's Mid$, Left$, etc.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Why the ending position is not the true ending position, synthax would be better. Is there any useful reason for that?.
It was a design decision for Java to use character offsets rather than lengths. It does make some things easier once you are used to it.
 
Upvote 0

DOM85

Active Member
Licensed User
Longtime User
It was a design decision for Java to use character offsets rather than lengths. It does make some things easier once you are used to it.

Yes i understand any decision taken by the creator of B4A, since he does what he wants.
I only say that it would be more evident that the ending position would be the last character position of the source string, rather than the last + 1, otherwise why the starting position is not the starting +1?

I think it may be a bug in the substring2 function for that reason. Be sure that i love B4A, and i respect very much its creators.
(I am lcreator or Pluton Language for mainframes).
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Yes i understand any decision taken by the creator of B4A, since he does what he wants.
No, it's how Java, which the language that Android applications are written in and what Basic4android is compiled into, implements specifying character spans in its string methods.
 
Upvote 0

jake

Member
Licensed User
Longtime User
My background is in vb.net, and I'm just getting started with B4A. I've been excited to find a software that allows me to get my feet wet with mobile app development while using coding and an IDE that are familiar.

I will say that with regards to substring2 I'm a little bit confused.

if a string has 3 characters, with indexes 0,1 and 2 and I want to select the last two characters using substring2, then do I use endindex=3?
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
My background is in vb.net, and I'm just getting started with B4A. I've been excited to find a software that allows me to get my feet wet with mobile app development while using coding and an IDE that are familiar.

I will say that with regards to substring2 I'm a little bit confused.

if a string has 3 characters, with indexes 0,1 and 2 and I want to select the last two characters using substring2, then do I use endindex=3?

For this you should use SubString (1)

SubString (BeginIndex As Int) As String
Returns a new string which is a substring of the original string.
The new string will include the character at BeginIndex and will extend to the end of the string.
 
Upvote 0

jake

Member
Licensed User
Longtime User
Okay yes, using substring instead of substring2 will allow me to get the last character of the string. Thanks for the help.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
To answer your original question
if a string has 3 characters, with indexes 0,1 and 2 and I want to select the last two characters using substring2, then do I use endindex=3?
SubString2(1, 3)

End index = Start index + number of characters required
 
Upvote 0
Top