修改字符串中的char [英] modify a char in string

查看:96
本文介绍了修改字符串中的char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


在C#中,我们不能直接修改字符串中的字符吗?我无法在任何地方找到这个方法。


我希望这样的东西能起作用,但它并没有


string myStr =" blah'';

myStr [3] =''e'';


提前致谢你的评论。

Hello,

Is it true that in C# we can''t modify directly a character in a string? I
can''t find the method anywhere.

I would expect something like this should work,but it doesn''t

string myStr = "blah'';
myStr[ 3 ] = ''e'';

Thanks in advance for your comment.

推荐答案

Cudnt找到一个直接的方法,但这里是间接方式之一


//未编译 - 可能有语法错误

string str =" blah";

char [] arr = str.ToCharArray();

arr [3] =''e'';

str = new string(arr);


HTH

rawCoder


" Zeng" <泽****** @ hotmail.com>在留言中写道

新闻:%2 ****************** @ TK2MSFTNGP14.phx.gbl ...
Cudnt find a direct method either but here is one of the indirect way

// Not Compiled-might have syntax errors
string str = "blah";
char[] arr = str.ToCharArray();
arr[3]=''e'';
str = new string( arr );

HTH
rawCoder

"Zeng" <Ze******@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
您好,

在C#中,我们不能直接修改字符串中的字符吗?我无法在任何地方找到这种方法。

我希望这样的东西能起作用,但它不会发出串联myStr =" blah'';
myStr [3] =''e'';

提前感谢您的评论。
Hello,

Is it true that in C# we can''t modify directly a character in a string? I
can''t find the method anywhere.

I would expect something like this should work,but it doesn''t

string myStr = "blah'';
myStr[ 3 ] = ''e'';

Thanks in advance for your comment.



你好,


这是正确的,你不能直接修改字符串,但是需要使用SubString重新组装它。 /插入/删除等


string myStr =" blah";

int pos = 3;

myStr = myStr .Substring(0,pos)+''c''+ myStr.Substring(++ pos,

myStr.Length - pos);


或你可以像rawCoder那样说并使用ToArray并更改char []


-

Happy Coding!

Morten Wennevik [C#MVP]
Hi Zeng,

It is correct, you cannot modify a string directly, but will need to
reassemble it, using SubString/Insert/Remove etc.

string myStr = "blah";
int pos = 3;
myStr = myStr.Substring(0, pos) + ''c'' + myStr.Substring(++pos,
myStr.Length - pos);

Or you can do as rawCoder said and use ToArray and change the char[]

--
Happy Coding!
Morten Wennevik [C# MVP]


Hello Zeng,


你不能使用字符串对象上的索引器来设置字符,只读它们。


希望这个ps,

RBischoff

------------------------------- ----------
http://msdn.microsoft .com / visualc / (VC ++ HOME)
http:// www.mvps.org/vcfaq/ (C ++ FAQ)
http ://www.winterdom.com/mcppfaq/ (MC ++常见问题解答)
http://msdn.microsoft.com/visualc/whidbey/ (CLI)


Z>您好,

Z>

Z>在C#中我们不能直接修改

Z>中的字符。串?我无法在任何地方找到这种方法。

Z>

Z>我希望这样的东西能起作用,但它不会是
Z>

Z> string myStr =" blah'';

Z> myStr [3] =''e'';

Z>在此先感谢您的评论。

Z>

Hello Zeng,

you can''t use the indexer on the string object to set chars, only read them.

Hope this helps,
RBischoff
-----------------------------------------
http://msdn.microsoft.com/visualc/ (VC++ HOME)
http://www.mvps.org/vcfaq/ (C++ FAQ)
http://www.winterdom.com/mcppfaq/ (MC++ FAQ)
http://msdn.microsoft.com/visualc/whidbey/ (CLI)

Z> Hello,
Z>
Z> Is it true that in C# we can''t modify directly a character in a
Z> string? I can''t find the method anywhere.
Z>
Z> I would expect something like this should work,but it doesn''t
Z>
Z> string myStr = "blah'';
Z> myStr[ 3 ] = ''e'';
Z> Thanks in advance for your comment.
Z>


这篇关于修改字符串中的char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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