- Home /
Add Text, remove vowels
Suppose the user typed a message into an input field. Is there a way for it to feed into a second feild, with Vowels and double letters removed? I had been provided these code snippets but not sure what to do with it honestly. lol
System.Text.RegularExpressions.Regex.Replace(inputString,"([a-zA-Z])\\1", "$1")
System.Text.RegularExpressions.Regex.Replace(inputString,"[aeiouAEIOU]*", string.Empty)
Answer by roojerry · Mar 18, 2015 at 09:51 PM
You probably want to learn about Regular Expressions.
The .Net Library has a class for handling things like Replacing strings matched with regular expression.
For your cases, I came up with these:
Replace all vowels with empty string:
System.Text.RegularExpressions.Regex.Replace(inputString,"[aeiouAEIOU]*", string.Empty)
Replace double letters with a single version of the letter:
System.Text.RegularExpressions.Regex.Replace(inputString,"([a-zA-Z])\\1", "$1")
Thanks dude for the references, honestly its beyond my know how to write c# for this... hopefully I can figure out what Im reading haah
yeah I'm completely lost here lol. Im unsure what Im even looking at. : (
Working from your above example:
string result = System.Text.RegularExpressions.Regex.Replace(userInputString,"[aeiouAEIOU]*", string.Empty);
userInputString would come from your first GUI text box. The Regex.Replace call is searching userInputString to find occurrences of lowercase/uppercase vowels and replacing those values with string.Empty(also represented by, ""). The result of this replace operation is then being stored in a new string variable called "result". You can then do whatever you want with that "result" string to display it in your GUI.
Answer by Jessespike · Mar 18, 2015 at 09:59 PM
Regex is probably a better solution, but if it helps. You can use string's Replace method:
string myText = "The quick brown fox jumps over the lazy dog";
// Make a copy of myText that will have characters replaced
string trimmed = myText;
// Using the String.Replace method
trimmed = trimmed.Replace("a", string.Empty);
trimmed = trimmed.Replace("e", string.Empty);
trimmed = trimmed.Replace("i", string.Empty);
trimmed = trimmed.Replace("o", string.Empty);
trimmed = trimmed.Replace("u", string.Empty);
// alternatively, you can chain the method
trimmed = allText.Replace("a", "")
.Replace("e", "")
.Replace("i", "")
.Replace("o", "")
.Replace("u", "");
// outputs "Th qck brwn fx jmps vr th lzy dg"
Debug.Log(trimmed);
Awesome thanks for the reply, personally I have no idea which method would be better right now. I will try anything haha. Im still a little loopy with the "newer" UI system. should the script be applied directly to the input feild?
Your answer
Follow this Question
Related Questions
comparing a number input to a letter input 3 Answers
Text input command terminal 2 Answers
Taking data from text file 2 Answers
GUI text Main Menu 1 Answer
write to a text file in resrouce folder 3 Answers