Click here to Skip to main content
15,030,781 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.
Will Sewell 23-Jul-21 10:04am
   
I was testing the function but I was only typing in lower case "o's" in the test as I didn't know it would be case sensitive like this. Initially I had no idea why it wasn't working as it wasn't removing any characters. After Simon's comment I realised that it was because I had the O capitalised but still didn't know how to remove both uppercase and lowercase O's. Then Simon gave me the solution to do it like this "[Oo]".
Richard MacCutchan 23-Jul-21 10:44am
   
It is obvious from your code that it was only testing upper case letters. A glance at the documentation makes it clear.
Will Sewell 23-Jul-21 10:52am
   
Obvious to you not obvious to me. I don't know what 'documentation' is.
Richard MacCutchan 23-Jul-21 12:12pm
   
This: Regex.Replace Method (System.Text.RegularExpressions) | Microsoft Docs[^].

But had you stated in the first place:
Source string: Out of the Orient.
After replace: ut of the rient.
we would have had a clue.

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)
   
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.
   
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
   

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