简体   繁体   English

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

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

I have a string in which each value is separated using "|"我有一个字符串,其中每个值都使用“|”分隔as following:如下:

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

I want to split this string at once without using array or split function in c# and want to store each splitted value in different variables so that i can use these variables for my code later for example:我想在不使用数组的情况下一次拆分此字符串或在 c# 中拆分 function 并希望将每个拆分后的值存储在不同的变量中,以便稍后我可以将这些变量用于我的代码,例如:

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

i have tried "indexOf" function but it is not working as expected.我试过"indexOf" function,但它没有按预期工作。

code which i have used:我用过的代码:

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

I would create a function that can get the value of a column by index, then reuse that as needed.我将创建一个 function 可以按索引获取列的值,然后根据需要重用它。 Here is an example:这是一个例子:

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

You're very close, you just have to make sure you're cutting off the part of the string that you used, and updating your index with the new location of the first |你非常接近,你只需要确保你切断了你使用的字符串部分,并使用第一个|的新位置更新你的索引。 in the string.在字符串中。 Changing the name to delimiterIndex just because calling a pipe a hyphen bugs me:P将名称更改为delimiterIndex只是因为调用 pipe 连字符会困扰我:P

Make special note that we have to go one past the index provided by IndexOf when doing our substring to make sure we have cut out the |请特别注意,在执行 substring 时,我们必须将 go 超过IndexOf提供的索引,以确保我们已删除| from the result string.从结果字符串。 When we provide the length for the substring, we must take this extra 1 character into account as it will reduce the overall length of the resultant string.当我们提供 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;

As Chris Dunaway suggested, this can be further refined to remove the substrings of the source string using the overload for IndexOf which takes a start index.正如 Chris Dunaway 所建议的,这可以进一步细化以使用 IndexOf 的重载来删除源字符串的子字符串,该重载采用起始索引。 This would look like the following这将如下所示

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

If I am allowed to answer with a free open source Nuget package, here is a quick example using WordParser.如果允许我使用免费的开源 Nuget package 来回答,这里有一个使用 WordParser 的简单示例。

Nuget Package: Nuget Package:

(.Net Framework) DataJuggler.Core.UltimateHelper (.Net 框架)DataJuggler.Core.UltimateHelper

(Dot Net Core) DataJuggler.UltimateHelper.Core (点网核心)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