i incounter substring problem
-
Hello guys, I'm halfway of my program when I encounter a problem. i'm trying to substring "test" string by this command(sample only): string strTest = "test"; MessageBox.Show(strTest.SubString(2, 3)); and I get this result: t My desired result is: st but I got my desired result with this code: string strTest = "test"; MessageBox.Show(strTest.SubString(2, 2)); I can't get why this happened. Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right? Thank you in advance...:)
-
Hello guys, I'm halfway of my program when I encounter a problem. i'm trying to substring "test" string by this command(sample only): string strTest = "test"; MessageBox.Show(strTest.SubString(2, 3)); and I get this result: t My desired result is: st but I got my desired result with this code: string strTest = "test"; MessageBox.Show(strTest.SubString(2, 2)); I can't get why this happened. Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right? Thank you in advance...:)
t e s t 0 1 2 3 SubString(initial Position , lenght) SubString(2, 2) is ok for get "st" with SubString(2,3) you got an exception (System.ArgumentOutOfRangeException)
-
t e s t 0 1 2 3 SubString(initial Position , lenght) SubString(2, 2) is ok for get "st" with SubString(2,3) you got an exception (System.ArgumentOutOfRangeException)
I think what he means is that SubString parameters are not begin and end position (like in Java), rather begining position and how much further from there, Ex Test 0123 SubString(2,2) - Te and two place further gives s and t. Third is out of arrays so System.ArgumentOutOfRangeException is thrown
-
Hello guys, I'm halfway of my program when I encounter a problem. i'm trying to substring "test" string by this command(sample only): string strTest = "test"; MessageBox.Show(strTest.SubString(2, 3)); and I get this result: t My desired result is: st but I got my desired result with this code: string strTest = "test"; MessageBox.Show(strTest.SubString(2, 2)); I can't get why this happened. Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right? Thank you in advance...:)
paulcortez wrote:
Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right?
Second parameter is number of characters in the string you want to display. If you want to display the last two characters in the string, leave off the second parameter. If you need to display a certain number of characters starting at 2 and you want to be sure it doesn't hit the out of range check the starting character plus number of chars you want to display against the strTest.Length value. If the starting plus number to display exceeds length, then you're out of bounds. Mike Poz
-
Hello guys, I'm halfway of my program when I encounter a problem. i'm trying to substring "test" string by this command(sample only): string strTest = "test"; MessageBox.Show(strTest.SubString(2, 3)); and I get this result: t My desired result is: st but I got my desired result with this code: string strTest = "test"; MessageBox.Show(strTest.SubString(2, 2)); I can't get why this happened. Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right? Thank you in advance...:)
public string Substring( int startIndex, int length ); damianbc
-
I think what he means is that SubString parameters are not begin and end position (like in Java), rather begining position and how much further from there, Ex Test 0123 SubString(2,2) - Te and two place further gives s and t. Third is out of arrays so System.ArgumentOutOfRangeException is thrown
Guys,,, Thank you so much. Now I understand that the second parameter is the lenght, not the position of the character where you want to end. Thankz so much..
-
paulcortez wrote:
Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right?
Second parameter is number of characters in the string you want to display. If you want to display the last two characters in the string, leave off the second parameter. If you need to display a certain number of characters starting at 2 and you want to be sure it doesn't hit the out of range check the starting character plus number of chars you want to display against the strTest.Length value. If the starting plus number to display exceeds length, then you're out of bounds. Mike Poz
Guys,,, Thank you so much. Now I understand that the second parameter is the lenght, not the position of the character where you want to end. Thankz so much..