C
C#14mo ago
Moataz

❔ How to censor a bad word in capital letters without changing original letters case?

I'm trying to loop through a list of bad words to filter them out, but when their case is different I had to use ToLower() which changes the case of original message completely to lower:
string msgWithBadWords;
foreach (string word in badList)
{
msgWithBadWords = msgWithBadWords.ToLower().Replace(word, "bad")
}
string msgWithBadWords;
foreach (string word in badList)
{
msgWithBadWords = msgWithBadWords.ToLower().Replace(word, "bad")
}
12 Replies
RumTery
RumTery14mo ago
just add another string
string msgWithBadWords;
string replacementWords;
foreach (string word in badList)
{
replacementWords = msgWithBadWords.ToLower().Replace(word, "bad")
}
string msgWithBadWords;
string replacementWords;
foreach (string word in badList)
{
replacementWords = msgWithBadWords.ToLower().Replace(word, "bad")
}
C# strings are immutable classes. So when you do something like
string a = "aaaa";
string b = a.Replace('a', 'b');
Console.WriteLine(a);
Console.WriteLine(b);
string a = "aaaa";
string b = a.Replace('a', 'b');
Console.WriteLine(a);
Console.WriteLine(b);
It will print: aaaa bbbb
Moataz
MoatazOP14mo ago
I want to retain the org msg case, which might be "THIS IS BAD MSG", ToLower() converts the whole msg to lower before replacing the bad word.
RumTery
RumTery14mo ago
string msgWithBadWords;
foreach (string word in badList)
{
msgWithBadWords = msgWithBadWords.Replace(word, "bad", StringComparison.OrdinalIgnoreCase)
}
string msgWithBadWords;
foreach (string word in badList)
{
msgWithBadWords = msgWithBadWords.Replace(word, "bad", StringComparison.OrdinalIgnoreCase)
}
Kouhai
Kouhai14mo ago
@Moataz Let's say if the word you want to censor is "BLUE" would it get replaced by "bad" or "BAD"?
Aokiri 🐸
Aokiri 🐸14mo ago
You could try with StringBuilder Since Strings in C# are immutable, StringBuilder in this case can help (they're mutable)
DbContext
DbContext14mo ago
Aokiri 🐸
Aokiri 🐸14mo ago
How did you created those links?
DbContext
DbContext14mo ago
Discord supports markdown
Discord
Markdown Text 101 (Chat Formatting: Bold, Italic, Underline)
Want to inject some flavor into your everyday text chat? You're in luck! Discord uses Markdown, a simple plain text formatting system that'll help you make your sentences stand out. Here's how to d...
DbContext
DbContext14mo ago
[String.Compare](https://learn.microsoft.com/en-us/dotnet/api/system.string.compare#system-string-compare(system-string-system-string-system-stringcomparison)
[String.Compare](https://learn.microsoft.com/en-us/dotnet/api/system.string.compare#system-string-compare(system-string-system-string-system-stringcomparison)
Aokiri 🐸
Aokiri 🐸14mo ago
Thanks a lot!
Angius
Angius14mo ago
You can use regex to do case-insensitive replace: https://stackoverflow.com/a/6276004/6042255
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server