I have a string representation exactly like 'ComputerName -- IPAddress'; i.e:
'samarena -- 192.168.1.97'
. I want to get only t开发者_如何学Che 'ComputerName' part from the actual representation by removing other characters. I'm actually quite beginner in using string.FormatMethods()
.
Please help me out.
Thanks.
This should do it:
string test = "samarena -- 192.168.1.97";
var result = test.Split(new string[] { "--" }, StringSplitOptions.None)[0].Trim();
Result will equal samarena
you could split the string on ' -- ' and then use the first part
This should do it.
var yourString = "samarena -- 192.168.1.97";
var indexOfDash = yourString.IndexOf("-");
var yourComputerName = yourString.SubString(0, indexOfDash).Trim();
But the other answers using Trim are better :)
This'd be the totally imperative way.
If You are sure there is always a substring " -- " after the part You want, You can do this
myString.Substring(0, myString.IndexOf(" -- "))
Or use a shorter part of " -- ".
Try this:
char [] chars = {'-'};
string test = "samarena -- 192.168.1.97";
//computerName array will have the Computer Name at the very first index (it is a zero based index
string[] computerName = test.Split(chars,StringSplitOptions.RemoveEmptyEntries);
//computerName[0] is your computerName
精彩评论