簡體   English   中英

使用string.Replace匹配整個單詞

[英]Use string.Replace to match whole words

我正在使用NET 2.0和WinForms。

當前,我需要一個代碼來在給定的文本中用另一個字符串替換一個字符串,但是在文本中它應該只查找整個單詞。 我的意思是:

string name = @"COUNTER = $40
CLOCK_COUNTER = $60";
name = name.Replace("COUNTER", "COUNT");

它僅應將COUNTER的第一個實例替換為COUNT ,因為這是整個單詞。 但是,它似乎是string.Replace並沒有考慮整個單詞。

請不要推薦正則表達式。 我已經嘗試過了,它太慢了,無法滿足我的需求。 我需要快速高效的工具。 我怎么能做到這一點?

string input = @"COUNTER = $40
CLOCK_COUNTER = $60";

string name = Regex.Replace(input, @"\bCOUNTER\b", "COUNT");

\\b標記單詞邊界。


Regex的唯一替代方法是開發自己的算法! 搜索“ COUNTER”,並測試前一個和后一個字符是否不是單詞字符。


編輯

這是我作為擴展方法的解決方案:

public static class ReplaceWordNoRegex
{
    private static bool IsWordChar(char c)
    {
        return Char.IsLetterOrDigit(c) || c == '_';
    }

    public static string ReplaceFullWords(this string s, string oldWord, string newWord)
    {
        if (s == null) {
            return null;
        }
        int startIndex = 0;
        while (true) {
            int position = s.IndexOf(oldWord, startIndex);
            if (position == -1) {
                return s;
            }
            int indexAfter = position + oldWord.Length;
            if ((position == 0 || !IsWordChar(s[position - 1])) && (indexAfter == s.Length || !IsWordChar(s[indexAfter]))) {
                s = s.Substring(0, position) + newWord + s.Substring(indexAfter);
                startIndex = position + newWord.Length;
            } else {
                startIndex = position + oldWord.Length;
            }
        }
    }
}

編輯#2:這是StringBuilder的解決方案。

public static string ReplaceFullWords(this string s, string oldWord, string newWord)
{
    if (s == null) {
        return null;
    }
    int startIndex = 0; // Where we start to search in s.
    int copyPos = 0; // Where we start to copy from s to sb.
    var sb = new StringBuilder();
    while (true) {
        int position = s.IndexOf(oldWord, startIndex);
        if (position == -1) {
            if (copyPos == 0) {
                return s;
            }
            if (s.Length > copyPos) { // Copy last chunk.
                sb.Append(s.Substring(copyPos, s.Length - copyPos));
            }
            return sb.ToString();
        }
        int indexAfter = position + oldWord.Length;
        if ((position == 0 || !IsWordChar(s[position - 1])) && (indexAfter == s.Length || !IsWordChar(s[indexAfter]))) {
            sb.Append(s.Substring(copyPos, position - copyPos)).Append(newWord);
            copyPos = position + oldWord.Length;
        }
        startIndex = position + oldWord.Length;
    }
}

小解決方法:

string name = @"COUNTER = $40
CLOCK_COUNTER = $60";
name=" "+name;
name = name.Replace(" COUNTER ", " COUNT ");

主要思想是,您必須用某種符號標記要替換的單詞,而這些符號是您不想替換的其他單詞沒有的

我認為您無法實現比RegExp更快的字符串替換(我說的是開發時間)

        string input = @"COUNTER = $40 CLOCK_COUNTER = $60";
        string pattern = @"\bCOUNTER\b";
        string replacement = "COUNT";
        var regex = new Regex(pattern,RegexOptions.Compiled);
        string result = regex.Replace(input, replacement);

如果要重用,則添加RegexOptions.Compiled可以使其速度更快

------------------- UPDATE -----------------------------

我記得這篇文章可能符合您的需求:

http://www.codeproject.com/KB/string/fastestcscaseinsstringrep.aspx

暫無
暫無

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

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