【问题标题】:split string and store it in another variable in c# [duplicate]拆分字符串并将其存储在c#中的另一个变量中[重复]
【发布时间】:2017-06-09 06:50:38
【问题描述】:

'\'之后的字符如何读取:

string PrName = "software\Plan Mobile"; 

【问题讨论】:

  • 问题是什么?
  • 从这个字符串“software\Plan Mobile”,我如何读取“\”之后的字符并使用 c# 将它们存储在不同的变量中
  • 查看我的建议

标签: c# .net


【解决方案1】:

首先别忘了@

 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('\\');

【讨论】:

    【解决方案2】:

    试试这个:

    char charToFind = '\';
    string PrName = "software\Plan Mobile";
    
    int indexOfChar = PrName.IndexOf(charToFind);
    
    if (indexOfChar >= 0)
    {
        string result = PrName.Substring(indexOfChar + 1);
    }
    

    输出:result = "Plan Mobile"

    【讨论】:

      【解决方案3】:

      我想,你想分割字符串

         string s = "software\Plan Mobile";
      
         // Split string on '\'.
         string[] words = s.Split('\');
      
         foreach (string word in words)
         {
            Console.WriteLine(word);
         }
      

      输出:

      software
      Plan mobile
      

      【讨论】:

        猜你喜欢
        • 2017-08-25
        • 1970-01-01
        • 2015-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-19
        • 1970-01-01
        • 2018-12-01
        相关资源
        最近更新 更多