Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Substring

Reply
Thread Tools

Substring

 
 
Jim McGivney
Guest
Posts: n/a
 
      05-15-2006
In an aspx (asp2.0) page in C# I have a string array. The array contains
letters separated into two groups by a dash (-). I want to obtain the
letters before the dash. I use the following code. Strangely, when the
array has a value of "MOD-AS" Label3 displays "3" while Label4 displays -1.
What's going on ??? What is changing the value of variable mn ? Thanks,
Jim

int i = 32;
string s1;
string s2;
s1 = Ante_Arr[i];
int mn = s1.IndexOf("-");
Label3.Text = mn.ToString(); // the displayed value is 3
try
{
s2 = s1.Substring(1, mn); // throws an error: length can not be less
than zero
}
catch
{
Label4.Text = mn.ToString(); // the displayed value is -1
}


 
Reply With Quote
 
 
 
 
Andrew Hayes
Guest
Posts: n/a
 
      05-15-2006
Maybe it's scope?

Put the int mn = s1.IndexOf("-"); line into the try block, or take a look at
the value of mn before the s2 = s1.Substring(1, mn); line.

"Jim McGivney" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> In an aspx (asp2.0) page in C# I have a string array. The array contains
> letters separated into two groups by a dash (-). I want to obtain the
> letters before the dash. I use the following code. Strangely, when the
> array has a value of "MOD-AS" Label3 displays "3" while Label4
> displays -1.
> What's going on ??? What is changing the value of variable mn ? Thanks,
> Jim
>
> int i = 32;
> string s1;
> string s2;
> s1 = Ante_Arr[i];
> int mn = s1.IndexOf("-");
> Label3.Text = mn.ToString(); // the displayed value is 3
> try
> {
> s2 = s1.Substring(1, mn); // throws an error: length can not be less
> than zero
> }
> catch
> {
> Label4.Text = mn.ToString(); // the displayed value is -1
> }
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding a SubString within a String Badass Scotsman ASP .Net 2 03-31-2006 04:00 PM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
find if there is a given substring inside a string juli ASP .Net 3 12-06-2004 11:52 AM
web sddress substring =?Utf-8?B?Sm9u?= ASP .Net 2 11-30-2004 01:31 PM
"Substring" for images? How do I determine if an image is contained within another? Sean Java 0 05-04-2004 05:50 PM



Advertisments