0

I have a string like "system\admin". Now I want get the "admin" part alone from the string using IndexOf() method.Can anyone please help me out.Thank you.

4
Contributors
9
Replies
10
Views
8 Years
Discussion Span
Last Post by Rashakil Fol
0

Hi,

Hope this helps. :)

string mystring = "system\admin";
int slash_position = mystring.IndexOf("\");
string string1 = mystring.substring(0,slash_position-1);
string string2 = mystring.substring(slash_position,mystring.length-1);

Regards

Votes + Comments
N/A
complete fail
0

Hi,

Hope this helps. :)

string mystring = "system\admin";
int slash_position = mystring.IndexOf("\");
string string1 = mystring.substring(0,slash_position-1);
string string2 = mystring.substring(slash_position,mystring.length-1);

Regards

Hi,

Thanks for your reply. I'm getting an error called "Newline in constant" in this line "int slash_position = mystring.IndexOf("\");"

0

Do you need to use the IndexOf member?

There are members that can more easily split strings seperated by special characters...

0

Do you need to use the IndexOf member?

There are members that can more easily split strings seperated by special characters...

0

Ah, the \ is a special character. Try

int slash_position = mystring.IndexOf("\\");

I'm getting an ArgumentOutOfRange Exception. "Length cannot be less than zero.
Parameter name: length"

0

Try understanding what the code does. This will allow you to find the fault. I suspect that I have made some stupid error in it, which you can easily figure out... :)

2
private void button1_Click(object sender, EventArgs e)
    {
      string domainAndUser = @"system\admin";
      string[] sArray = domainAndUser.Split(new char[] { '\\' }, StringSplitOptions.None);
      string domain = sArray[0];
      string user = sArray[1];
      Console.WriteLine("Domain: " + domain);
      Console.WriteLine("User: " + user);
    }

Results in:

Domain: system
User: admin
Votes + Comments
lol StringSplitOptions.None
Efficient as always!
-1

Try understanding what the code does. This will allow you to find the fault. I suspect that I have made some stupid error in it, which you can easily figure out... :)

Bah, I meant to uprep this post with "complete win" while downrepping the first, but forgot you could only rep somebody once per day. :(

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.