Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

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

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


comment is thanks to phil.o
 
Share this answer
 
try the below code

TextBox1.Text = "NEED HELP";
string text = TextBox1.Text.Replace(" ", "_");
string text1 = text;
 
Share this answer
 
Answered only to remove from unanswered queue: solved by OP.
 
Share this answer
 
You just forgot assign result of Replace operation to text. Everything else was/is fine.

C#
text = text.Replace(" ", "_");
 
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