知道要替换的索引时,String.Replace方法不可用 [英] String.Replace method not usable when knowing index to replace

查看:90
本文介绍了知道要替换的索引时,String.Replace方法不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




只想将字符串索引1处的字符替换为另一个

字符。只想替换该位置的角色。我想

替换方法会重载一个索引参数,你可以在那个位置编写想要的字符。但不,替换

方法只允许将一个已知字符替换为另一个已知字符。

问题是我不知道要替换的角色,只需要在已知索引处替换

角色。


这是我找到的第一个解决方法:


string s =" ihj";

s = s.Remove(1,1) 。插入(1," p");


这是第二个:


string s =" ihj" ;;

char [] array = s.ToCharArray();

array [1] =''p'';

s = new string(数组);


有点复杂,你不觉得吗?我认为String.Replace应该是

重载了这样的东西:


string替换(int index,char newCharacter);


发表评论吗?


提前致谢。

Hi,

Just want to replace character at index 1 of a string with another
character. Just want to replace character at that position. I thought
Replace method would be overloaded with an index parameter with which
you can write wanted character at that position. But no, Replace
method only allows replacing one known character with another. The
problem is I don''t know the character to replace, just must replace
the character at a known index.

This is the first workaround I''ve found:

string s = "ihj";
s = s.Remove(1, 1).Insert(1, "p");

And here is the second one:

string s = "ihj";
char[] array = s.ToCharArray();
array[1] = ''p'';
s = new string(array);

A bit complicated,, don''t you think? I think String.Replace should be
overloaded with something like this:

string Replace (int index, char newCharacter);

Any comments?

Thanks in advance.

推荐答案

" ; Lonifasiko" < ml ******* @ gmail.comwrote:
"Lonifasiko" <ml*******@gmail.comwrote:

只想替换

字符串的索引1处的字符与另一个角色。
Just want to replace character at index 1 of a
string with another character.



使用StringBuilder而不是字符串。


Eq。

Use StringBuilder instead of string.

Eq.


Lonifasiko写道:
Lonifasiko wrote:




只想将字符串的索引1处的字符替换为另一个

字符。只想替换该位置的角色。我想

替换方法会重载一个索引参数,你可以在那个位置编写想要的字符。但不,替换

方法只允许将一个已知字符替换为另一个已知字符。

问题是我不知道要替换的角色,只需要在已知索引处替换

角色。


这是我找到的第一个解决方法:


string s =" ihj";

s = s.Remove(1,1) 。插入(1," p");


这是第二个:


string s =" ihj" ;;

char [] array = s.ToCharArray();

array [1] =''p'';

s = new string(数组);


有点复杂,你不觉得吗?我认为String.Replace应该是

重载了这样的东西:


string替换(int index,char newCharacter);


有任何意见吗?


提前致谢。
Hi,

Just want to replace character at index 1 of a string with another
character. Just want to replace character at that position. I thought
Replace method would be overloaded with an index parameter with which
you can write wanted character at that position. But no, Replace
method only allows replacing one known character with another. The
problem is I don''t know the character to replace, just must replace
the character at a known index.

This is the first workaround I''ve found:

string s = "ihj";
s = s.Remove(1, 1).Insert(1, "p");

And here is the second one:

string s = "ihj";
char[] array = s.ToCharArray();
array[1] = ''p'';
s = new string(array);

A bit complicated,, don''t you think? I think String.Replace should be
overloaded with something like this:

string Replace (int index, char newCharacter);

Any comments?

Thanks in advance.



问题在于它并不那么容易。字符串是不可变的,所以你在改变它时总是要创建一个新的字符串,你不能用b $ b来改变一个现有的字符串。


我认为当你以这种方式更改字符串中的字符时,你通常会更改多个字符,因此字符串类中的方法

不是很有用。它宁愿产生误导,导致人们无意中创造了许多中间字符串而没有意识到它。


这是另一种方法:


StringBuilder builder = new StringBuilder;

builder.Chars [1] =''p'';

s = builder .ToString();


StringBuilder在内部使用可变字符串。当您调用

ToString时,您将获得该字符串作为常规不可变字符串,因此

没有额外的中间字符串或数组与其他字符串一起创建

你提出的方法。


-

G?ran Andersson

_____
http://www.guffa.com


StringBuilder提供了Replace方法重载,但仍然强迫我知道要替换哪个字符。
。请参阅MSDN帮助:


- StringBuilder.Replace(Char,Char,Int32,Int32)在此实例的
子字符串中替换所有出现的a指定字符

与另一个指定字符。


- StringBuilder.Replace(String,String,Int32,Int32)替换,

在这个实例的子串中,所有出现的指定

字符串都带有另一个指定字符串。


问题是我不知道哪个是字符必须是

替换;我只知道它的位置!我已经尝试了这两种方法

传递null,String.Empty等作为第一个参数,但

在执行期间崩溃。

StringBuilder sb = new StringBuilder(" ihj");

sb = sb.Replace(null," p",1,1); //无法指定h必须是

更改因为位置1的原因它并不总是h


还有其他任何想法吗?


问候。

StringBuilder offers Replace method overload, but still forces me to
know which character I want to replace. See MSDN help:

- StringBuilder.Replace (Char, Char, Int32, Int32) Replaces, within a
substring of this instance, all occurrences of a specified character
with another specified character.

- StringBuilder.Replace (String, String, Int32, Int32) Replaces,
within a substring of this instance, all occurrences of a specified
string with another specified string.

The problem is I don''t know which is the character that must be
replaced; I only know its position! I''ve tried both these methods
passing null, String.Empty and so on as the first parameter, but
crashes during execution.

StringBuilder sb = new StringBuilder("ihj");
sb = sb.Replace(null, "p", 1, 1); // Cannot specify "h" must be
changed cause character at position 1 it''s not always "h"

Any other ideas?

Regards.


这篇关于知道要替换的索引时,String.Replace方法不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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