【问题标题】:how to get the substring如何获取子字符串
【发布时间】:2013-03-30 09:24:41
【问题描述】:

我有一个类似 1234ABCD-1A-AB 的字符串,我在 string[] 分隔符中有分隔符,我正在循环直到字符串的长度。我想得到substring。在循环里面我写下面的代码

string tempVar = test.Substring(0, test.IndexOf("'" + separator+ "'"));

我也试过这样

string tempVar = String.Join(",", test.Split(',').Select(s => s.Substring(0, s.IndexOf("'" + separator+ "'"))));

通过使用这个我得到错误索引不应该小于 0,循环将只运行 2 次,因为我是基于分隔符的循环,并且我的字符串中有 2 个分隔符。

让我解释一下:

我有一个分隔符循环,它只会执行 2 次,因为我将 2 个分隔符,一个是第 9 个位置,另一个是第 14 个位置,在该循环内,我根据分隔符拆分字符串

string[] test1 = test.Split("'" + separator+ "'");

在我的下一步中,我将像这样为下一个过程传递一个字符串值

string temp = test1[i].ToString();

有了这个,我只得到 2 个字符串,即 1234ABCD1A 我想在循环中也得到第三个值。所以我想到了取子字符串而不是使用拆分。

输出应该是:

first time: 1234ABCD

second time: 1A

third time: AB

【问题讨论】:

  • 您对问题的编辑不清楚。你想达到什么目的?

标签: c# winforms substring


【解决方案1】:

您可以将Split 与分隔符'-' 一起使用,然后访问返回的string[]

string[] parts = test.Split('-');
string firstPart  = parts[0];
string secondPart = parts.ElementAtOrDefault(1);
string thirdPart  = parts.ElementAtOrDefault(2);

Demo

【讨论】:

  • 哇!我以前从未使用过ElementAtOrDefault。太酷了蒂姆! +1 当然..
【解决方案2】:

使用拆分功能:

string s = "1234ABCD-1A-AB";
string[] parts = s.Split('-');

然后:

s[0] == "1234ABCD"
s[1] == "1A"
s[2] == "AB"

根据现在更新的要求,尝试以下方法:

string input = "1234ABCD-1A-AB";
char separator = '-';

string[] parts = input.Split(separator);

// if you do not need to know the item index:
foreach (string item in parts)
{
    // do something here with 'item'
}

// if you need to know the item index:
for (int i = 0; i < parts.Length; i++)
{
    // do something here with 'item[i]', where i is 
    // the index (so 1, 2, or 3 in your case).
}

【讨论】:

    【解决方案3】:
    string[] items = str.Split(new char[] { '-' });
    

    【讨论】:

    • 如果我将使用拆分,那么如果我将通过循环索引 `string temp = test1[i].ToString();,我将如何获得第三个值
    • 我已经给出了一些解释,请检查
    • 还是不清楚。尝试发布您的输入数据和整个代码。和 "'" + separator+ "'" ({"'"} 部分)有什么关系?
    【解决方案4】:

    通过String.Split()很简单:

    string t = "1234ABCD-1A-AB";
    string[] tempVar = t.Split('-');
    
    foreach(string s in tempVar)
    {
         Console.WriteLine(s);
    }
    Console.Read();
    

    打印:

    1234ABCD 1A AB

    【讨论】:

    • 反对者愿意发表评论吗? @Rocky,您需要清楚地布置您的要求。
    【解决方案5】:

    你可以使用String.Split():

    string str = "1234ABCD-1A-AB";
    string[] splitted = str.Split('-');
    /* foreach (string item in splitted)
    {
        Console.WriteLine(item);
    }*/
    

    您可以将其设置为:

     string firstPart = splitted.FirstOrDefault();
     string secondPart = splitted.ElementAtOrDefault(1);
     string thirdPart = splitted.ElementAtOrDefault(2);
    

    【讨论】:

      【解决方案6】:

      你可以使用String.Split方法

      返回一个字符串数组,其中包含此实例中的子字符串 由指定字符串或 Unicode 的元素分隔的 字符数组。

      string s = "1234ABCD-1A-AB";
      string[] items = s.Split('-');
      
      for(int i = 0; i < items.Length; i++)
        Console.WriteLine("Item number {0} is: {1}", i, items[i]);
      

      输出将是;

      Item number 0 is: 1234ABCD
      Item number 1 is: 1A
      Item number 2 is: AB
      

      这是DEMO

      【讨论】:

        【解决方案7】:

        我只是缺少索引

        string tempVar = test.Substring(0, test.IndexOf(separator[0].ToString()));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-25
          • 1970-01-01
          • 2011-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-25
          相关资源
          最近更新 更多