拆分字符串并将其存储在c#中的另一个变量中

时间:2017-06-09 06:50:38

标签: c# .net

如何在'\'之后阅读字符:

string PrName = "software\Plan Mobile"; 

3 个答案:

答案 0 :(得分:2)

首先,不要忘记@

 string PrName = @"software\Plan Mobile"; 

接下来,如果您只想要尾部(即"Plan Mobile"),那么Substring将会:

 // if no '\' found, the entire string will be return
 string tail = PrName.Substring(PrName.IndexOf('\\') + 1);

如果您想要两者(所有部分),请尝试Split

 // parts[0] == "software"
 // parts[1] == "Plan Mobile"
 string[] parts = PrName.Split('\\');

答案 1 :(得分:0)

试试这个:

char charToFind = '\';
string PrName = "software\Plan Mobile";

int indexOfChar = PrName.IndexOf(charToFind);

if (indexOfChar >= 0)
{
    string result = PrName.Substring(indexOfChar + 1);
}

输出:result = "Plan Mobile"

答案 2 :(得分:0)

我想,你想拆分字符串

   string s = "software\Plan Mobile";

   // Split string on '\'.
   string[] words = s.Split('\');

   foreach (string word in words)
   {
      Console.WriteLine(word);
   }

<强>输出:

software
Plan mobile