C
C#5mo ago
Sticky

Tidying and making a repeat column faster

Hi, I'm making an website and i need to format string so that every symbol is formatted into its appropiate counterpart like shown below:
text = text.Replace(" ", "-");
text = text.Replace("ı", "i");
text = text.Replace("ğ", "g");
text = text.Replace("ü", "u");
text = text.Replace("ö", "o");
text = text.Replace("ş", "s");
text = text.Replace("ç", "c");
text = text.Replace("_", "-");
text = text.Replace("/", "-");
/*
more under here
*/
text = text.Replace(" ", "-");
text = text.Replace("ı", "i");
text = text.Replace("ğ", "g");
text = text.Replace("ü", "u");
text = text.Replace("ö", "o");
text = text.Replace("ş", "s");
text = text.Replace("ç", "c");
text = text.Replace("_", "-");
text = text.Replace("/", "-");
/*
more under here
*/
how can i make this code far cleaner and faster?
11 Replies
Salman
Salman5mo ago
How large is the string
Sticky
StickyOP5mo ago
it will be an url i dont think it will be long but this code looks very ugly the issue is there will be a lot of replace in here string wont be that long, at least it isnt a paragraph
Salman
Salman5mo ago
I'm not on my laptop but if the string Isn't that large as you are saying then I'd do it in a single pass by creating a new string instead of replacing multiple times . You would need a lengthy switch case for that ... That won't be much cleaner but can be faster You can also use dictionary mapping perhaps in some way regex or what not
Sticky
StickyOP5mo ago
True What do you mean by dictionary mapping I cant get the gist of mapping atm I also closed my laptop :p So like:
public static string ToURL(string Url){
return Regex.Replace(Url, @"+[^A-Z0-9]+", "");
public static string ToURL(string Url){
return Regex.Replace(Url, @"+[^A-Z0-9]+", "");
Like this?
Salman
Salman5mo ago
c#
private readonly Dictionary<char, char> Replacements = new()
{
{'ı', 'i'},
{'ğ', 'g'},
{'ü', 'u'},
{'ö', 'o'},
{'ş', 's'},
{'ç', 'c'},
....
};
c#
private readonly Dictionary<char, char> Replacements = new()
{
{'ı', 'i'},
{'ğ', 'g'},
{'ü', 'u'},
{'ö', 'o'},
{'ş', 's'},
{'ç', 'c'},
....
};
So you make a dictionary like this and and then iterate through the string and replace the keys with the values like this:
c#
foreach (var kvp in Replacements)
{
text = text.Replace(kvp.Key, kvp.Value);
}
c#
foreach (var kvp in Replacements)
{
text = text.Replace(kvp.Key, kvp.Value);
}
It's a bit cleaner right?
Sticky
StickyOP5mo ago
Nvm im derp Yea it is cleaner
Salman
Salman5mo ago
Yeah dictionary is great for such use cases
Sticky
StickyOP5mo ago
Didnt think about this approach at all, thanks for helping
Salman
Salman5mo ago
Np 👍
mindhardt
mindhardt5mo ago
Performance-wise I would not recommend this It will allocate a new string for every replaced char I would stick to
Regex.Replace(url, "[^A-Z0-9]", match => Replacements.GetValueOrDefault(match.Value) ?? match.Value)
Regex.Replace(url, "[^A-Z0-9]", match => Replacements.GetValueOrDefault(match.Value) ?? match.Value)
Salman
Salman5mo ago
I don't know regex yet so I never said it was more performant but it was cleaner so. Ofc regex solutions can be better . It's doing the same thing as the original code .
Want results from more Discord servers?
Add your server