如何在不使用数组或C#中的拆分函数的情况下拆分字符串

时间:2019-11-11 20:08:04

标签: c# winforms substring

我有一个字符串,其中每个值都用“ |”分隔如下:

698301    | 48380.80                   | sam                            | aass@gmail.com                 | 5675767        | 3     | 40602.80           | 7778 

我想立即拆分此字符串而不在c#中使用数组或拆分函数,并希望将每个拆分后的值存储在不同的变量中,以便稍后例如在我的代码中使用这些变量:

  

a = 69801,b = 48380.80,c = sam,d = aass@gmail.com,e = 5675767,f = 3,g = 40602.80,h = 7778

我尝试了"indexOf"函数,但无法正常工作。

我使用的代码:

        var hyphenIndex = str.IndexOf("|");
        var a = str.Substring(0, hyphenIndex);
        MessageBox.Show(a.ToString());

        var b = str.Substring(1, hyphenIndex);
        MessageBox.Show(b.ToString());

        var c = str.Substring(2, hyphenIndex);
        MessageBox.Show(c.ToString());

        var d = str.Substring(3, hyphenIndex);
        MessageBox.Show(d.ToString());

        var e = str.Substring(4, hyphenIndex);
        MessageBox.Show(e.ToString());

        var f = str.Substring(5, hyphenIndex);
        MessageBox.Show(f.ToString());

        var g = str.Substring(6, hyphenIndex);
        MessageBox.Show(g.ToString());

        var h = str.Substring(7, hyphenIndex);
        MessageBox.Show(h.ToString());

2 个答案:

答案 0 :(得分:3)

我将创建一个可以按索引获取列值的函数,然后根据需要重新使用它。这是一个示例:

public string GetValue(string row, int index)
{
    var start = 0;
    for (var i = 0; i < index; i++)
        start = row.IndexOf('|', start + 1);
    var end = row.IndexOf('|', start + 1);
    if (end == -1)
        end = row.Length;
    return row.Substring(start, end - start)
              .Replace("|", string.Empty).Trim();
}

var str = "698301 | 48380.80 | sam | aass@gmail.com | 5675767 | 3 | 40602.80 | 7778";
var a = GetValue(str, 0);
var b = GetValue(str, 1);
var c = GetValue(str, 2);
var d = GetValue(str, 3);
var e = GetValue(str, 4);
var f = GetValue(str, 5);
var g = GetValue(str, 6);
var h = GetValue(str, 7);

答案 1 :(得分:1)

您非常接近,您只需要确保截去了所用字符串的一部分,并使用字符串中第一个|的新位置来更新索引即可。将名称更改为delimiterIndex仅仅是因为给管道打连字符会打扰我:P

请特别注意,在执行子字符串时,我们必须比IndexOf提供的索引更远,以确保已从结果字符串中切出|。当我们提供子字符串的长度时,我们必须考虑这个额外的1个字符,因为这会减少结果字符串的总长度。

    string s = "698301 | 48380.80 | sam | aass@gmail.com | 5675767 | 3 | 40602.80 | 7778";

    string a, b, c, d, e, f, g, h;

    int delimiterIndex = s.IndexOf('|');
    a = s.Substring(0, delimiterIndex);
    s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);

    delimiterIndex = s.IndexOf('|');
    b = s.Substring(0, delimiterIndex);
    s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);

    delimiterIndex = s.IndexOf('|');
    c = s.Substring(0, delimiterIndex);
    s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);

    delimiterIndex = s.IndexOf('|');
    d = s.Substring(0, delimiterIndex);
    s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);

    delimiterIndex = s.IndexOf('|');
    e = s.Substring(0, delimiterIndex);
    s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);

    delimiterIndex = s.IndexOf('|');
    f = s.Substring(0, delimiterIndex);
    s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);

    delimiterIndex = s.IndexOf('|');
    g = s.Substring(0, delimiterIndex);
    s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);

    h = s;

正如Chris Dunaway所建议的那样,可以使用IndexOf的重载对其进行进一步细化以删除源字符串的子字符串,该重载采用起始索引。看起来像下面的

    string s = "698301 | 48380.80 | sam | aass@gmail.com | 5675767 | 3 | 40602.80 | 7778";

    string a, b, c, d, e, f, g, h;

    int lastIndex = 0;
    int delimiterIndex = s.IndexOf('|', 0);
    a = s.Substring(lastIndex, delimiterIndex);
    lastIndex = delimiterIndex;

    delimiterIndex = s.IndexOf('|', delimiterIndex+1);
    b = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
    lastIndex = delimiterIndex;

    delimiterIndex = s.IndexOf('|', delimiterIndex+1);
    c = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
    lastIndex = delimiterIndex;

    delimiterIndex = s.IndexOf('|', delimiterIndex+1);
    d = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
    lastIndex = delimiterIndex;

    delimiterIndex = s.IndexOf('|', delimiterIndex+1);
    e = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
    lastIndex = delimiterIndex;

    delimiterIndex = s.IndexOf('|', delimiterIndex+1);
    f = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
    lastIndex = delimiterIndex;

    delimiterIndex = s.IndexOf('|', delimiterIndex+1);
    g = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
    lastIndex = delimiterIndex;

    h = s.Substring(lastIndex + 1);