Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm trying to write a function to replace all the O's in a string. Not sure where i'm going wrong.

Remove the O - Album on Imgur[^]

Edit: I realised that it wasn't working because of it not removing lower case O's. Is there a way to write the function so that it removes both upper and lower case or should I force it be written in lower case in the textbox?

What I have tried:

C#
private void simpleButton_Remove_Click(object sender, EventArgs e)
{
	string Answer = ReplaceO(textBox_Word.Text);

	label_WordResult.Text = Answer;
}

private string ReplaceO(string O)
{
	string ReturnValue = "";

	ReturnValue = Regex.Replace(O, "O", "");

	return ReturnValue;
}
Posted
Updated 23-Jul-21 3:11am
v3
Comments
Simon_Whale 23-Jul-21 8:54am    
What is happening when you run it? does answer get the correct value?
Will Sewell 23-Jul-21 9:01am    
Sorry, I have now edited my question. I realised why it wasn't working for me before to some extent.
Richard MacCutchan 23-Jul-21 9:33am    
In future please explain exactly what your problem is. Had you said "it does not remove the lowercase letters", we would not have needed to waste time.
Will Sewell 23-Jul-21 9:37am    
I didn't know that was the problem.
Richard MacCutchan 23-Jul-21 9:43am    
You had a source string with some characters, and a result which you knew to be incorrect. So you must have known why it was incorrect or you would never have posted the question.

couple of modifications I would do

1. change the variable name from O to something more meaningful
2. in the regex part i would change "O" to "^[Oo]" (the ^ tells it to start at the beginning of the string)
 
Share this answer
 
Comments
Will Sewell 23-Jul-21 9:17am    
This worked for me. Thank you!
Quote:
Not sure where i'm going wrong.

Since you have not explained what the problem is we don't know either.

But I just tried that code and it works fine.
 
Share this answer
 
Did you have a look at the documentation?

Regex.Replace Method (System.Text.RegularExpressions) | Microsoft Docs[^]
There is an overloaded Replace method
Replace(String, String, String, RegexOptions)

RegexOptions are described here:
RegexOptions Enum (System.Text.RegularExpressions) | Microsoft Docs[^]

You need
RegexOptions.IgnoreCase
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900