使用与string.replace匹配整个单词 [英] Use string.Replace to match whole words

查看:269
本文介绍了使用与string.replace匹配整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET 2.0和WinForms。



目前,我需要一个代码与另一个在一个给定的文本替换字符串,但在它的文字应该只找全字。我的意思是:



 字符串名称= @COUNTER = $ 40个
CLOCK_COUNTER = $ 60;
NAME = name.Replace(COUNTER,COUNT);



它应该只替换 COUNTER的第一个实例 COUNT ,因为这是整个单词。然而,似乎与string.replace 不采取整字考虑。



请不建议正则表达式。我已经试了一下,这是我的需要过于缓慢。我需要的东西非常快速和有效。我怎么能做到这一点?


解决方案

 字符串输入= @COUNTER = $ $ 40 b $ b CLOCK_COUNTER = $ 60; 

字符串名称= Regex.Replace(输入,@\bCOUNTER\b,COUNT);



\b 标记词边界值。






要正则表达式的唯一选择是开发自己的算法!搜索COUNTER的测试不是一个单词字符前面和后面的字符。






修改



下面是我作为扩展方法解决方案:

 公共静态类ReplaceWordNoRegex ​​
{
私人静态布尔IsWordChar(焦三)
{
返回Char.IsLetterOrDigit(三)|| ç=='_';
}

公共静态字符串ReplaceFullWords(这个字符串s,串oldWord,串newWord)
{
如果(S == NULL){
返回空值;
}
INT的startIndex = 0;
,而(真){
INT位置= s.IndexOf(oldWord,则startIndex);
如果(位置== -1){
返回小号;
}
INT indexAfter =位置+ oldWord.Length;
如果((位置== 0 || IsWordChar(S [位置 - 1]))及!及(indexAfter == || s.Length IsWordChar(S [indexAfter]))){
S = s.Substring(0位)+ newWord + s.Substring(indexAfter);
的startIndex =位置+ newWord.Length;
}其他{
的startIndex =位置+ oldWord.Length;
}
}
}
}






编辑#2:
和这里是StringBuilder的一个解决方案。

 公共静态字符串ReplaceFullWords(这个字符串s,串oldWord,串newWord)
{
如果(S == NULL){
返回NULL;
}
INT的startIndex = 0; //当我们开始以秒来搜索。
INT copyPos = 0; //当我们开始从s复制到某人。
变种某人=新的StringBuilder();
,而(真){
INT位置= s.IndexOf(oldWord,则startIndex);
如果(位置== -1){
如果(copyPos == 0){
返回小号;
}
如果(s.Length> copyPos){//复制最后一个大块。
sb.Append(s.Substring(copyPos,s.Length - copyPos));
}
返回sb.ToString();
}
INT indexAfter =位置+ oldWord.Length;
如果((位置== 0 || IsWordChar(S [位置 - 1]))及!及(indexAfter == || s.Length IsWordChar(S [indexAfter]))){
sb​​.Append(s.Substring(copyPos,位置 - copyPos))追加(newWord)。
copyPos =位置+ oldWord.Length;
}
的startIndex =位置+ oldWord.Length;
}
}


I'm using NET 2.0 and WinForms.

Currently, I need a code to replace a string with another one in a given text, but in the text it should only look for whole words. What I mean is:

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

It should only replace the first instance of COUNTER with COUNT, because that's whole word. However, it seems string.Replace does not take whole word into consideration.

Please don't recommend regex. I have already tried it, and it's too slow for my needs. I need something very fast and efficient. How could I accomplish this?

解决方案

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

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

\b marks word boundries.


The only alternative to Regex is to develop your own algorithm! Search for "COUNTER" and test the previous and following character for not being a word character.


EDIT:

Here is my solution as extension method:

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


EDIT #2: And here is a solution with 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.replace匹配整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆