String.Replace ignoring case
You could use a Regex and perform a case insensitive replace:
class Program
{
static void Main()
{
string input = "hello WoRlD";
string result =
Regex.Replace(input, "world", "csharp", RegexOptions.IgnoreCase);
Console.WriteLine(result); // prints "hello csharp"
}
}
How to ignore case in String.replace
Use a regular expression:
var regex = new Regex( "camel", RegexOptions.IgnoreCase );
var newSentence = regex.Replace( sentence, "horse" );
Of course, this will also match words containing camel, but it's not clear if you want that or not.
If you need exact matches you can use a custom MatchEvaluator.
public static class Evaluators
{
public static string Wrap( Match m, string original, string format )
{
// doesn't match the entire string, otherwise it is a match
if (m.Length != original.Length)
{
// has a preceding letter or digit (i.e., not a real match).
if (m.Index != 0 && char.IsLetterOrDigit( original[m.Index - 1] ))
{
return m.Value;
}
// has a trailing letter or digit (i.e., not a real match).
if (m.Index + m.Length != original.Length && char.IsLetterOrDigit( original[m.Index + m.Length] ))
{
return m.Value;
}
}
// it is a match, apply the format
return string.Format( format, m.Value );
}
}
Used with the previous example to wrap the match in a span as:
var regex = new Regex( highlightedWord, RegexOptions.IgnoreCase );
foreach (var sentence in sentences)
{
var evaluator = new MatchEvaluator( match => Evaluators.Wrap( match, sentence, "<span class='red'>{0}</span>" ) );
Console.WriteLine( regex.Replace( sentence, evaluator ) );
}
Replace String ignoring case in Java
String.replace()
doesn't support regex. You need String.replaceAll()
.
DeleteLine.replaceAll("(?i)" + Pattern.quote(Checkout), "");
C# Replace string case insensitive
If the answer from the duplicate question does't help you, here is the code in your case (notice I removed the while
loop - the condition in it is false if casing is different and also you don't really need it):
foreach (string fWord in FilteredWords)
{
Input = Regex.Replace(Input, fWord, "****", RegexOptions.IgnoreCase);
}
For example, the code below
string fWord = "abc";
input = "AbC";
input = Regex.Replace(input, fWord, "****", RegexOptions.IgnoreCase);
produces the value ****
.
How to do a case-insensitive string replacement
Could use a regular expression.
Just add (?i) before your string to ignore case.
So for example:
multiword.getString().replaceAll ( "(?i)radha", "sai");
how to replace a string ignoring case?
Without Regex, I don't know.
With Regex, it would give you something like this :
var regex = new Regex( str, RegexOptions.IgnoreCase );
var newSentence = regex.Replace( sentence, "new value" );
I found an interesting article here, with a sample code that looks to work faster than Regex.Replace : http://www.codeproject.com/KB/string/fastestcscaseinsstringrep.aspx
Javascript | case-insensitive string replace
With a case-insensitive regular expression:
function boldString(str, find) { var reg = new RegExp('('+find+')', 'gi'); return str.replace(reg, '<b>$1</b>');}console.log(boldString('Apple', 'ap'))
How to replace case-insensitive literal substrings in Java
String target = "FOOBar";
target = target.replaceAll("(?i)foo", "");
System.out.println(target);
Output:
Bar
It's worth mentioning that replaceAll
treats the first argument as a regex pattern, which can cause unexpected results. To solve this, also use Pattern.quote
as suggested in the comments.
Is there an alternative to string.Replace that is case-insensitive?
From MSDN
$0 - "Substitutes the last substring matched by group number number (decimal)."
In .NET Regular expressions group 0 is always the entire match. For a literal $ you need to
string value = Regex.Replace("%PolicyAmount%", "%PolicyAmount%", @"$$0", RegexOptions.IgnoreCase);
JavaScript replace all ignoring case sensitivity
You will get the result you want using capture groups and the i
(ignoreCase) modifier. You can reference the capture group with $1
.
var result = "Cooker Works";var searchterm = "cooker wor";
searchterm.split(" ").forEach(function(item) { result = result.replace(new RegExp(`(${item})`, 'ig'), "<strong>$1</strong>");});
console.log(result)
Related Topics
How to Make Linq Execute a (Sql) Like Range Search
An Error Occurred During Report Processing. -Rldc Reporting in ASP.NET MVC
Dynamically Added Dropdownlists Are Not Firing Selectedindexchanged Event
Jquery Ajax Call to an ASP.NET Webmethod
How to Decode String to Xml String in C#
Does C# Support Project-Wide Default Namespace Imports Like Vb.Net
Deserialize JSON into Object C#
Finding a Subsequence in Longer Sequence
C# Printing Problem (Richtextbox)
JSON.Net Serialize Directly from Oledbconnection
How to List Available Video Modes Using C#
Is There Any Difference Between Datetime in C# and Datetime in SQL Server
Regex Split String But Keep Separators
Play Multiple Sounds Using Soundplayer
Determine List of Event Handlers Bound to Event