簡體   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