簡體   English   中英

字符串操作-最簡單的C#方法

[英]String operation — Easiest way C#

實施以下方案的最佳方法是什么

string sample = "Mountain View XX,Lake"

預計2種情況為o / p

山景湖

我可以通過sample.split[','][1]到達湖泊

什么是讓Mountain View ignoring XX的最佳方法是什么

我嘗試了多個拆分並將它們也與lastIndex串聯在一起

那么還有其他更簡單的方法嗎?

string.Substring方法可以正常工作:

sample = sample.Substring(0, sample.IndexOf(","));

還有一個string.LastIndexOf ,直到子字符串的最后一次出現,例如:

sample = sample.Substring(0, sample.LastIndexOf(","));

在您的樣品,都將在相同的工作,因為只有一個,焦炭。

之后,您可以刪除直到最后 空格字符。

sample = sample.Substring(0, sample.LastIndexOf(" "));

使用String.Replace

sample= sample.Replace("XX", "");

嘗試使用正則表達式。 (\\ w + \\ s +?\\ w +)\\ s +?XX \\,(\\ w +) http://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex.aspx組1包含Mountain View組2包含Lake

您說您嘗試過多次拆分。 您可以在單個string.Split()指定多個分隔符:

var parts
    = sample.Split(new[] { "XX", "," }, StringSplitOptions.RemoveEmptyEntries);

現在您有兩個部分:

var part1 = parts[0];  // Mountain View
var part2 = parts[1];  // Lake

目前尚不清楚為什么要刪除字符串的確切部分,但是這里有一些不同的方法可以刪除字符串:

string sample = "Mountain View XX,Lake"

// option 1
string removed = sample.Remove(14, 3);

// option 2
string replaced = sample.Replace("XX,", String.Empty);

// option 3
string[] parts = sample.Split(',');
parts[0] = parts[0].Substring(0, 14);
string concatenated = String.Concat(parts);

如果字符串的XX部分確實是其他內容,例如街道編號,那么您將需要一些內容來確定要刪除多少字符串。 這里有些例子:

string sample = "Mountain View 123,Lake"

// option 1; remove digits followed by a comma
string replaced = Regex.Replace(sample, "\d+,", String.Empty);

// option 2; remove what's after the last space in the part before the comma
string[] parts = sample.Split(',');
parts[0] = parts[0].Substring(0, parts[0].LastIndexOf(" ") + 1);
string concatenated = String.Concat(parts);

你遺漏了很多東西。 XX一定是XX嗎? 您是否保證格式正確,或者您信任用戶輸入? 如果您知道想要所有開頭字符,直到遇到雙大寫字母組合,則可以執行一個非常簡單的正則表達式。 我必須查找它,因為我已經使用它們已有多年了,但這將非常簡單。

這可以解決問題。 它確實包括兩個XX之前的空格。 我假設XX可以是任何大寫字母。

    Regex reg;
    reg = new Regex(".{1,}(?=[A-Z]{2})");
    var output = reg.Match("Mountain View XX, Lake");
    string text = output.Value;

您可以在此處進行測試: http : //www.regexplanet.com/advanced/dotnet/index.html,並在此處了解有關正則表達式的更多信息: http : //www.regular-expressions.info/tutorial.html

我建議您選擇:

string sample = "Mountain View XX,Lake";
Console.WriteLine(sample.Split(',')[1]);// Lake
Console.WriteLine(sample.Remove(sample.IndexOf("XX,")).Trim()); // Mountain View

演示: http//ideone.com/RizzzC

我可能會做這樣的事情:

public static string[] MyCustomSplit(string input, List<string> reservedWords)
{
    List<string> outputLines = new List<string>();

    string[] lines = input.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

    foreach (string line in lines)
    {
        if (!string.IsNullOrWhiteSpace(line))
        {
            string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            List<string> lineParts = new List<string>();

            foreach (string part in parts)
            {
                if (!reservedWords.Contains(part))
                {
                    lineParts.Add(part);
                }
            }

            outputLines.Add(string.Join(" ", lineParts.ToArray()));
        }
    }

    return outputLines.ToArray();
}

接着

string sample = "Mountain View XX,Lake";
List<string> reservedWords = new List<string>() { "XX" };
string[] test = MyCustomSplit(sample, reservedWords);

結果是:

string[0] = Mountain View
string[1] = Lake

或類似這樣的東西:

public static string[] MyCustomSplitAndCleanup(string input)
{
    List<string> outputLines = new List<string>();

    string[] lines = input.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

    foreach (string line in lines)
    {
        if (!string.IsNullOrWhiteSpace(line))
        {
            string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            List<string> lineParts = new List<string>();

            for (int index = 0; index < parts.Length; index++ )
            {
                string part = parts[index];

                int numericValue = 0;

                bool validPart = true;

                if (int.TryParse(part, out numericValue))
                {
                    if (index == 0 || index == parts.Length - 1)
                    {
                        validPart = false;
                    }
                }

                if (validPart)
                {
                    lineParts.Add(part);
                }
            }

            outputLines.Add(string.Join(" ", lineParts.ToArray()));
        }
    }

    return outputLines.ToArray();
}

涵蓋了這一點:

string sample1 = "Mountain View 32,Lake";
string sample2 = "17 Park place,Something";

string[] test1 = MyCustomSplitAndCleanup(sample1);
string[] test2 = MyCustomSplitAndCleanup(sample2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM