【发布时间】:2017-06-09 06:50:38
【问题描述】:
'\'之后的字符如何读取:
string PrName = "software\Plan Mobile";
【问题讨论】:
-
问题是什么?
-
从这个字符串“software\Plan Mobile”,我如何读取“\”之后的字符并使用 c# 将它们存储在不同的变量中
-
查看我的建议
'\'之后的字符如何读取:
string PrName = "software\Plan Mobile";
【问题讨论】:
首先别忘了@:
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('\\');
【讨论】:
试试这个:
char charToFind = '\';
string PrName = "software\Plan Mobile";
int indexOfChar = PrName.IndexOf(charToFind);
if (indexOfChar >= 0)
{
string result = PrName.Substring(indexOfChar + 1);
}
输出:result = "Plan Mobile"
【讨论】:
我想,你想分割字符串
string s = "software\Plan Mobile";
// Split string on '\'.
string[] words = s.Split('\');
foreach (string word in words)
{
Console.WriteLine(word);
}
输出:
software
Plan mobile
【讨论】: