DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

.Substring and .Remove

By: rekha singh | 10 May 2010 12:20 pm

 Hi,

 
I am getting confused with my strings. If I have the statements
 
intPos = 9
strString = "abcdefghi"
strChar = strString.Substring (intPos, 1)
 
I get an error because intPos is beyond the end of the string despite the string being nine characters long and I want the nineth one. If I change it to
 
strChar = strString.Substring (intPos - 1, 1)
 
it works fine. I am also having the same problem with the Remove method in the fact I have to subtract one to get it to remove the correct character.
 
Is this correct? And why do I have to do it?
 
Looking forward to your comments.
 

Comments

Hi,
 
 
String is nothing but a character array.
 
Always array starts with 0.
 
for eg.
 
char s[10];
 
The array would be s[0],s[1],.. ...s[9].
 
If you need to get first character you should mention s[0] not s[1].
 
If you need to get last character you should mention s[9] not s[10].

 

By: rekha singh | 10 May 2010
intPos = 9
strString = "abcdefghi"
strChar = strString.Substring (intPos, 1)
 
Actually , there is nothing wrong at your string, may be the way you read that string that have to be changed
 
strString = "abcdefghi"
 
if i write your string's position, it will be like this
 
strString = "a b c d  e f  g h  i"
Position =   0 1 2 3 4 5 6 7 8
 
So there is nostring @pos 9
 
 

Thanks 

By: rekha singh | 10 May 2010
Hello, the reason you are having that problem is because VB.Net uses a 0 based number system which means that instead of starting with one when you count it starts with 0 so "a" is 0, "b" is 1, "c" is 2. You can just change your intPos = 8 instead of 9 and it will work fine. The same goes with the remove statement and everything else.

 

By: rekha singh | 10 May 2010

Have you tried the Mid function?

 
strString = "abcdefghi"
strChar = Mid(strString, 9,1) ' would return the letter i
 
strChar = Mid(strString, 1,1) ' would return the letter a
 
> intPos = 9
> strString = "abcdefghi"
> strChar = strString.Substring (intPos, 1)
 
By: rekha singh | 10 May 2010

 It is because the index is zero based. The first character occupies position
"0"

http://msdn. microsoft. com/en-us/ library/aa904307 %28v=VS.71% 29.aspx

By: rekha singh | 10 May 2010

By default, array indicies start from 0.

0, 1, 2, 3, .., X
 
By: rekha singh | 10 May 2010

Greetings,


The positions of the characters in the string start with the zero position. Another way to say it is that the string is a zero based array of characters.
 
Regards,
 
By: rekha singh | 10 May 2010
Hello,
 
intPos = 9
strString = "abcdefghi"
strChar = strString.Substring (intPos - 1, 1)
 
The above code is correct if you want the 9th character. Because you need to specify index and the index always starts from zero or you can say index is always 1 less than actual position.
 
 
The function "substring" works on indexing basis. Therefore, your first letter "a" is at index 0 and ur last letter "i" is at index 8. The same applies for the remove method too. Remove method is also based on index not the actual position, therefore, If you want to remove any letter, you need to specify the index (actual position -1) not the actual position. Therefore you must state the position based on index not based on actual position.
 
There's one more function called MID in vb.net which does not work on index but on the actual position, So the same code can be written as:
 
intPos = 9
strString = "abcdefghi"
strChar = Mid(strString, 9, 1)
 
I hope I made you clear on this.
 

regards, 

By: rekha singh | 10 May 2010

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move