C
C#β€’9mo 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
RumTeryβ€’9mo 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
Moatazβ€’9mo 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
RumTeryβ€’9mo 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
Kouhaiβ€’9mo ago
@Moataz Let's say if the word you want to censor is "BLUE" would it get replaced by "bad" or "BAD"?
Aokiri 🐸
Aokiri πŸΈβ€’9mo ago
You could try with StringBuilder Since Strings in C# are immutable, StringBuilder in this case can help (they're mutable)
DbContext
DbContextβ€’9mo ago
Aokiri 🐸
Aokiri πŸΈβ€’9mo ago
How did you created those links?
DbContext
DbContextβ€’9mo 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
DbContextβ€’9mo 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 πŸΈβ€’9mo ago
Thanks a lot!
Angius
Angiusβ€’9mo ago
You can use regex to do case-insensitive replace: https://stackoverflow.com/a/6276004/6042255
Accord
Accordβ€’9mo 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
More Posts