用字符替换单词之间的空格 [英] replacing spaces between words with character

查看:112
本文介绍了用字符替换单词之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



小问题我有

(否则我不会问:)



我需要用另一个字符替换文本框中的空格,我试过



string.Replace



喜欢这个



string text = textbox.Text;



textbox .Text =需要帮助;

text.Replace(,_);

string text1 = text;



但没有任何反应



而不是,_我试过'','_',仍然没有,我试过字符串。空,_,仍然没什么,它会抛出一个错误,那个旧值不能为零长度





我只是想当我在文本框中键入一些文本时,当我按空格键分隔单词时,字符_就在那里,那就是

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

推荐答案

你只是忘了指定Replace操作的结果到文本。其他一切都很好。



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

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


string.Replace函数返回一个必须分配给变量的字符串。



解决方案是
string.Replace function returns a string that has to be assigned to variable.

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





评论是由于phil.o



comment is thanks to phil.o


仅回答从未答复的队列中删除:由OP解决。
Answered only to remove from unanswered queue: solved by OP.


这篇关于用字符替换单词之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆