Способ иметь String.Replace только ударить "целые слова"
Мне нужен способ:
"test, and test but not testing. But yes to test".Replace("test", "text")
верните это:
"text, and text but not testing. But yes to text"
В основном я хочу заменить целые слова, но не частичные совпадения.
ПРИМЕЧАНИЕ. Мне нужно использовать VB для этого (код SSRS 2008), но С# - это мой обычный язык, поэтому ответы в любом случае прекрасны.
Ответы
Ответ 1
Регулярное выражение - самый простой способ:
string input = "test, and test but not testing. But yes to test";
string pattern = @"\btest\b";
string replace = "text";
string result = Regex.Replace(input, pattern, replace);
Console.WriteLine(result);
Важной частью шаблона является метасимвол \b
, который соответствует границам слов. Если вам нужно, чтобы это было нечувствительным к регистру, используйте RegexOptions.IgnoreCase
:
Regex.Replace(input, pattern, replace, RegexOptions.IgnoreCase);
Ответ 2
Я создал функцию (см. сообщение в блоге здесь), которая обертывает выражение регулярного выражения, предложенное Ахмад Магед
/// <summary>
/// Uses regex '\b' as suggested in https://stackoverflow.com/questions/6143642/way-to-have-string-replace-only-hit-whole-words
/// </summary>
/// <param name="original"></param>
/// <param name="wordToFind"></param>
/// <param name="replacement"></param>
/// <param name="regexOptions"></param>
/// <returns></returns>
static public string ReplaceWholeWord(this string original, string wordToFind, string replacement, RegexOptions regexOptions = RegexOptions.None)
{
string pattern = String.Format(@"\b{0}\b", wordToFind);
string ret=Regex.Replace(original, pattern, replacement, regexOptions);
return ret;
}
Ответ 3
Как прокомментировал Sga, решение регулярных выражений не является совершенным. И я думаю, что это не очень удобно.
Вот мой вклад:
public static class StringExtendsionsMethods
{
public static String ReplaceWholeWord ( this String s, String word, String bywhat )
{
char firstLetter = word[0];
StringBuilder sb = new StringBuilder();
bool previousWasLetterOrDigit = false;
int i = 0;
while ( i < s.Length - word.Length + 1 )
{
bool wordFound = false;
char c = s[i];
if ( c == firstLetter )
if ( ! previousWasLetterOrDigit )
if ( s.Substring ( i, word.Length ).Equals ( word ) )
{
wordFound = true;
bool wholeWordFound = true;
if ( s.Length > i + word.Length )
{
if ( Char.IsLetterOrDigit ( s[i+word.Length] ) )
wholeWordFound = false;
}
if ( wholeWordFound )
sb.Append ( bywhat );
else
sb.Append ( word );
i += word.Length;
}
if ( ! wordFound )
{
previousWasLetterOrDigit = Char.IsLetterOrDigit ( c );
sb.Append ( c );
i++;
}
}
if ( s.Length - i > 0 )
sb.Append ( s.Substring ( i ) );
return sb.ToString ();
}
}
...
С тестовыми примерами:
String a = "alpha is alpha";
Console.WriteLine ( a.ReplaceWholeWord ( "alpha", "alphonse" ) );
Console.WriteLine ( a.ReplaceWholeWord ( "alpha", "alf" ) );
a = "alphaisomega";
Console.WriteLine ( a.ReplaceWholeWord ( "alpha", "xxx" ) );
a = "aalpha is alphaa";
Console.WriteLine ( a.ReplaceWholeWord ( "alpha", "xxx" ) );
a = "alpha1/alpha2/alpha3";
Console.WriteLine ( a.ReplaceWholeWord ( "alpha", "xxx" ) );
a = "alpha/alpha/alpha";
Console.WriteLine ( a.ReplaceWholeWord ( "alpha", "alphonse" ) );
Ответ 4
Я просто хочу добавить примечание об этом конкретном шаблоне регулярного выражения (используется как в принятом ответе, так и в функции ReplaceWholeWord).
Это не работает, если то, что вы пытаетесь заменить, - это не слово.
Здесь тестовый пример:
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
string input = "doin' some replacement";
string pattern = @"\bdoin'\b";
string replace = "doing";
string result = Regex.Replace(input, pattern, replace);
Console.WriteLine(result);
}
}
(готов попробовать код: http://ideone.com/2Nt0A)
Это нужно учитывать, особенно если вы выполняете пакетные переводы (например, я сделал для некоторых работ i18n).
Ответ 5
Код С# (консольное приложение)
класс Программа
{
static void Main(string[] args)
{
Console.Write("Please input your comment: ");
string str = Console.ReadLine();
string[] str2 = str.Split(' ');
replaceStringWithString(str2);
Console.ReadLine();
}
public static void replaceStringWithString(string[] word)
{
string[] strArry1 = new string[] { "good", "bad", "hate" };
string[] strArry2 = new string[] { "g**d", "b*d", "h**e" };
for (int j = 0; j < strArry1.Count(); j++)
{
for (int i = 0; i < word.Count(); i++)
{
if (word[i] == strArry1[j])
{
word[i] = strArry2[j];
}
Console.Write(word[i] + " ");
}
}
}
}
Ответ 6
Если вы хотите определить, какие символы составляют слово i.e. "_" и "@"
вы можете использовать мою функцию (vb.net):
Function Replace_Whole_Word(Input As String, Find As String, Replace As String)
Dim Word_Chars As String = "[email protected]"
Dim Word_Index As Integer = 0
Do Until False
Word_Index = Input.IndexOf(Find, Word_Index)
If Word_Index < 0 Then Exit Do
If Word_Index = 0 OrElse Word_Chars.Contains(Input(Word_Index - 1)) = False Then
If Word_Index + Len(Find) = Input.Length OrElse Word_Chars.Contains(Input(Word_Index + Len(Find))) = False Then
Input = Mid(Input, 1, Word_Index) & Replace & Mid(Input, Word_Index + Len(Find) + 1)
End If
End If
Word_Index = Word_Index + 1
Loop
Return Input
End Function
Test
Replace_Whole_Word("We need to replace words tonight. Not to_day and not too well to", "to", "xxx")
Результат
"We need xxx replace words tonight. Not to_day and not too well xxx"
Ответ 7
Вы можете использовать string.replace
string input = "test, and test but not testing. But yes to test";
string result2 = input.Replace("test", "text");
Console.WriteLine(input);
Console.WriteLine(result2);
Console.ReadLine();