Click here to Skip to main content
14,265,748 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello,

little issue I have
(otherwise I wouldn't be asking :)

I need to replace blank spaces in textbox with another character, I tried

string.Replace

like this

string text = textbox.Text;

textbox.Text = "NEED HELP";
text.Replace(" ", "_");
string text1 = text;

but nothing happens

instead of " ", "_" I tried ' ', '_', still nothing, I tried string.Empty, "_", still nothin, it throws an error on that one that he old value can not be zero length


I just want to while I type in textbox some text when I hit spacebar to divide words a character _ comes there, that's all
Posted
Comments
shonezi 27-Dec-13 5:46am
   
forget I asked, found a solution, and not my credit of course for this idiotic question I had

text1 = text.Replace(@" ", "_");

so stupid for asking while people have serious issues to deal with
phil.o 27-Dec-13 5:51am
   
Indeed, string.Replace function returns a string that you have to assign to your variable.
It can't be used the way you did first time; it does not modify the provided source-string.
You should mark this question as answered; and better: create an answer to this question explaining the problem and its solution. This way someone might better understand the correct usage of string.Replace method.
Cheers.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You just forgot assign result of Replace operation to text. Everything else was/is fine.

text = text.Replace(" ", "_");
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

string.Replace function returns a string that has to be assigned to variable.

solution is
text1 = text.Replace(@" ", "_");


comment is thanks to phil.o
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Answered only to remove from unanswered queue: solved by OP.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

try the below code

TextBox1.Text = "NEED HELP";
string text = TextBox1.Text.Replace(" ", "_");
string text1 = text;
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100