-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
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"
.
3
Try using the code below.
String s = "@@bb@@@";
s = s.Replace("@", "A");
The Replace method of the String class will replace all occurrences of @ with A. I recommend you study the documentation below as well.
https://docs.microsoft.com/en-us/dotnet/api/system.string.replace?view=net-5.0
1
How you specified the tag regex 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 ... :/
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
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:
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() + "!!!!";
}
}
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?
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 c# regex
You are not signed in. Login or sign up in order to post.
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", "...")– gleisin-dev