我有一个名为hello world的字符串
我需要把"world"换成" chsharp "
我用:
string.Replace("World", "csharp");
但结果是,字符串没有被替换。原因在于区分大小写。原来的字符串包含“世界”,而我试图取代“世界”。
有没有办法避免字符串中的这种区分大小写的情况?替代方法?
我有一个名为hello world的字符串
我需要把"world"换成" chsharp "
我用:
string.Replace("World", "csharp");
但结果是,字符串没有被替换。原因在于区分大小写。原来的字符串包含“世界”,而我试图取代“世界”。
有没有办法避免字符串中的这种区分大小写的情况?替代方法?
当前回答
我推荐StringComparison。CurrentCultureIgnoreCase方法由ZZY / Gama Sharma提出。这是另一个可以用于LINQ的技术:
List<string> ItemsToRedact = new List<string> {"star", "citizen", "test", "universe"}; string Message =“就像每颗恒星都是独一无二的,但却造就了宇宙,你身上的光芒造就了你”; List<string> ReplacementList =消息。(' ')。Where(x => itemstoredata . contains (x. tolower ())).ToList(); foreach(替换列表中的变量) { Message = Message。替换(单词,“[已编辑]”); } Console.WriteLine(消息);
就像每个人都是独一无二的一样,你身上的光芒也造就了你
这段代码可以进一步提炼,但为了可读性,我将其分解了
其他回答
你可以使用Regex并执行不区分大小写的替换:
class Program
{
static void Main()
{
string input = "hello WoRlD";
string result =
Regex.Replace(input, "world", "csharp", RegexOptions.IgnoreCase);
Console.WriteLine(result); // prints "hello csharp"
}
}
我推荐StringComparison。CurrentCultureIgnoreCase方法由ZZY / Gama Sharma提出。这是另一个可以用于LINQ的技术:
List<string> ItemsToRedact = new List<string> {"star", "citizen", "test", "universe"}; string Message =“就像每颗恒星都是独一无二的,但却造就了宇宙,你身上的光芒造就了你”; List<string> ReplacementList =消息。(' ')。Where(x => itemstoredata . contains (x. tolower ())).ToList(); foreach(替换列表中的变量) { Message = Message。替换(单词,“[已编辑]”); } Console.WriteLine(消息);
就像每个人都是独一无二的一样,你身上的光芒也造就了你
这段代码可以进一步提炼,但为了可读性,我将其分解了
(编辑:没有意识到“裸链接”的问题,对不起)
从这里开始:
string myString = "find Me and replace ME";
string strReplace = "me";
myString = Regex.Replace(myString, "me", strReplace, RegexOptions.IgnoreCase);
似乎你不是第一个抱怨字符串不区分大小写的人。
修改了@Darky711的答案,使用传入的比较类型和匹配框架替换命名和xml注释尽可能接近。
/// <summary>
/// Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
/// </summary>
/// <param name="str">The string performing the replace method.</param>
/// <param name="oldValue">The string to be replaced.</param>
/// <param name="newValue">The string replace all occurrances of oldValue.</param>
/// <param name="comparisonType">Type of the comparison.</param>
/// <returns></returns>
public static string Replace(this string str, string oldValue, string @newValue, StringComparison comparisonType)
{
@newValue = @newValue ?? string.Empty;
if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(oldValue) || oldValue.Equals(@newValue, comparisonType))
{
return str;
}
int foundAt;
while ((foundAt = str.IndexOf(oldValue, 0, comparisonType)) != -1)
{
str = str.Remove(foundAt, oldValue.Length).Insert(foundAt, @newValue);
}
return str;
}
我写了扩展方法:
public static string ReplaceIgnoreCase(this string source, string oldVale, string newVale)
{
if (source.IsNullOrEmpty() || oldVale.IsNullOrEmpty())
return source;
var stringBuilder = new StringBuilder();
string result = source;
int index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);
while (index >= 0)
{
if (index > 0)
stringBuilder.Append(result.Substring(0, index));
if (newVale.IsNullOrEmpty().IsNot())
stringBuilder.Append(newVale);
stringBuilder.Append(result.Substring(index + oldVale.Length));
result = stringBuilder.ToString();
index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);
}
return result;
}
我在前面的扩展方法中使用了两个额外的扩展方法:
public static bool IsNullOrEmpty(this string value)
{
return string.IsNullOrEmpty(value);
}
public static bool IsNot(this bool val)
{
return val == false;
}