繁体   English   中英

如何在不使用 arrays 或在 c# 中拆分 function 的情况下拆分字符串

[英]How to split a string without using arrays or split function in c#

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

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

我想在不使用数组的情况下一次拆分此字符串或在 c# 中拆分 function 并希望将每个拆分后的值存储在不同的变量中,以便稍后我可以将这些变量用于我的代码,例如:

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

我试过"indexOf" function,但它没有按预期工作。

我用过的代码:

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

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

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);

你非常接近,你只需要确保你切断了你使用的字符串部分,并使用第一个|的新位置更新你的索引。 在字符串中。 将名称更改为delimiterIndex只是因为调用 pipe 连字符会困扰我:P

请特别注意,在执行 substring 时,我们必须将 go 超过IndexOf提供的索引,以确保我们已删除| 从结果字符串。 当我们提供 substring 的长度时,我们必须考虑这个额外的 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);

如果允许我使用免费的开源 Nuget package 来回答,这里有一个使用 WordParser 的简单示例。

Nuget Package:

(.Net 框架)DataJuggler.Core.UltimateHelper

(点网核心)DataJuggler.UltimateHelper.Core

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

// use this char to split on
char[] delimiterChars = { '|' };

// get the words
List<Word> words = WordParser.GetWords(source, delimiterChars);

// If the words collection exists and has one or more items
if (ListHelper.HasOneOrMoreItems(words))
{   
    // Iterate the collection of Word objects
    foreach (Word word in words)
    {
        // Do something with each word
       Console.WriteLine(word.Text);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM