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.
scvinod
0
Newbie Poster
Recommended Answers
Jump to PostHi,
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
Jump to PostAh, the \ is a special character. Try
int slash_position = mystring.IndexOf("\\");
Jump to Postprivate 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
All 9 Replies
glasnoster
5
Newbie Poster
Rashakil Fol
commented:
complete fail
-1
kvprajapati
commented:
N/A
+6
scvinod
0
Newbie Poster
glasnoster
5
Newbie Poster
glasnoster
5
Newbie Poster
glasnoster
5
Newbie Poster
scvinod
0
Newbie Poster
glasnoster
5
Newbie Poster
sknake
1,622
Senior Poster
Featured Poster
ddanbe
commented:
Efficient as always!
+5
Rashakil Fol
commented:
lol StringSplitOptions.None
+6
Rashakil Fol
978
Super Senior Demiposter
Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.