(C#) How can I replace a specific character "x" with "y" in a String?

Asked

Viewed 90 times

-1

To want to make a function that can replace a value in character( "@" ) with a value "A" inside the string. Assuming the input is "@@bbcc@@", then the function returns "AAbbccAA" .

  • 1

    If it is specific characters, for example @ for A, use the function Replace of C#, like this: String str = "@@bbcc@"; str = str. Replace("@", "A"). Replace("Character", "Letter"). Replace("Character", "...")

3 answers

3

1

How you specified the tag in your question, to do the same thing using regular expression would be the equivalent of:

using System.Text.RegularExpressions;

string s = "@@bbb@";

string S = Regex.Replace(s, @"@", "b");
  • I used the class Regularexpressions tb, but I could not use it in an organized way to solve my problem ... :/

  • 1

    What was the problem you had @causticroot?

  • I found a confusing solution, I read the documentation but these classes that manipulate String have very similar behaviors... as I started studying a little bit I ended up getting confused a little. I shared below how I resolved using the Regex.Replace

  • 1

    If you have questions about the method that was confused, share with us a new question.

0

What I was solving was this:

Gordon Ramsay shouts. He shouts and swears. There may be Something Wrong with him.

Anyway, you will be Given a string of four words. Your job is to turn in them to Gordon language.

Rules:

Obviously the words should be Caps, Every word should end with '!!!! ', Any Letter 'a' or 'A' should become '@', Any other vowel should become '*'.

I ended up finding an example with the use of class Stringbuild and was the most readable way to resolve, I will share here the paths I followed:

the first, kind of embarrassing and confusing:

public class Kata
{
  public static string Gordon(string a)
  {
    a = System.Text.RegularExpressions.Regex.Replace(a, "[eiou]", "*", RegexOptions.IgnoreCase).Replace("[aA]", "@");
    return a = System.Text.RegularExpressions.Regex.Replace(a, "a", "@", RegexOptions.IgnoreCase).Replace(" ", "!!!! ").ToUpper() + "!!!!";
  }
}

The second, more understandable :

public class Kata
{
    public static string Gordon(string a)
    {
        StringBuilder frase = new StringBuilder(a);
        frase.Replace("a", "@")
            .Replace("e", "*")
            .Replace("i", "*")
            .Replace("o", "*")
            .Replace("u", "*")
            .Replace(" ", "!!!! ");
        string fraseNova = string.Format("{0}!!!!", frase).ToUpper();
        return fraseNova;
        
    }

}
  • A rocket launcher against an ant. All this logic is necessary for such simple work that is already ready in the Framework?

  • 1

    Remembering that my intention is to understand how the process worked, and for that I tried to look for an example that would help me understand better. Still study, the means to solve was at the level of knowledge, not to seek the solution less expensive or the most correct, but I understood your point of view, I recognize that there are better ways to solve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.