字符串 - 优化问题.. [英] String - Optimization questions..

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

问题描述

你好! :O)


我需要将字符串的所有重音字符替换为

非重音字符等效字符。


1.有没有办法这样做而不用迭代整个字符串和

替换字符,例如转换或编码类?

2.如果没有,你怎么能在没有重新创建新字符串的情况下实际替换字符串中的特定字符




现在我做类似的事情..

// ***

string s =" aaaa ccaaa c aaa ca aaa" ;;

char [] chars = s .ToCharArray();

for(int i = 0; i< chars.Length; i ++)

{

if(chars [ i] ==''c'')

{

chars [i] =''x'';

}

s =新字符串(字符);

}

// ***


这似乎是是我们发现做的更轻松的方式...:O /


我会做这样的事情,但它不是可能相反,但是

编译器抱怨索引器是只读的.....(这可能是b $ b穷人......)

// ***

string s =" aaaa ccaaa c aaa ca aaa" ;;


for(int i = 0;我< s.Lenght; i ++)

{

if(s [i] ==''c'')

{

s [i] ='''x'';

}

}

// ***


我不**寻找Replace()函数...


感谢您的帮助..


-

最好的问候

Yanick Lefebvre


请将答案发布到小组,以便所有人都能受益

解决方案

为什么不想要Replace功能呢?它的创建是为了你不会像你在那里那样写b循环。


如果你必须使用循环,为什么你每个循环在
结尾处重新创建字符串?有什么意义?你总是只使用最后一个,所以

一旦循环结束就从char数组中重新创建字符串,没有理由

为20个字符做20次。

" Zoury" < yanick_lefebvre at hotmail dot com>在消息中写道

新闻:%2 ****************** @ TK2MSFTNGP09.phx.gbl ...

您好! :O)

我需要用字符串替换字符串中所有重点字符的
非重音符号。

1.是否存在一种方法这样做而不用迭代整个字符串和
替换字符,例如转换或编码类?
2.如果没有,你怎么能真正替换字符串中的特定字符
现在我做的事情就像..
// ***
字符串s =" aaaa ccaaa c aaa ca aaa" ;;
char [] chars = s.ToCharArray();
for(int i = 0; i< chars.Length; i ++)
{
if(chars [i] ==''c'')
{
chars [i] =''x'';
}
s = new string(chars);
}
// ***

这似乎是我们发现的更轻松的方式......:O /

我会做的像这样的东西,但它不可能,但
编译器抱怨索引器被读取ly .....(可怜......)
// ***
字符串s =" aaaa ccaaa c aaa ca aaa" ;;
for(int i = 0;我< s.Lenght; i ++)
{
if(s [i] ==''c'')
{
s [i] =''x'';
}
}
// ***

我**不**寻找Replace()函数...

感谢帮助..

-
最好的问候
Yanick Lefebvre

请将答案发布到小组,以便所有人都能受益



Zoury

字符串是不可变的。如果你想替换字符串中的字符,那么必须创建一个新字符串


Tu-Thac


----- Zoury写道:----


你好! :O


我需要用它替换字符串中所有重点字符''

非重音字符等效

1.有没有办法这样做而不用迭代整个字符串

替换字符,例如转换或编码类

2。如果没有,你怎么能在没有重新创建新字符串的情况下实际替换strin

中的特定字符


现在我做类似的事情。

// **

string s =" aaaa ccaaa c aaa ca aaa"

char [] chars = s.ToCharArray()

for(int i = 0; i< chars.Length; i ++


if(chars [i] ==''c''


chars [i] =''x''


s = new string(chars)


// **


这似乎是我们发现的更轻松的方式...:O


我会做类似的事情但是这不可能,但是你需要付出代价呃正在抱怨索引器是只读的.....(我差点... b $ b差...

// **

string s =" aaaa ccaaa c aaa ca aaa"


for(int i = 0;我< s.Lenght; i ++


if(s [i] ==''c''


s [i] =''x''

// **


我不**寻找Replace()函数..


感谢您的帮助。


-

最佳评价

Yanick Lefebvr


请将答案发布到论坛,以便所有人都能受益


>字符串是不可变的。如果你想替换字符串中的字符,

然后必须创建一个新字符串。


hhmmm ...:O /

...感谢信息......:O )


-

最好的问候

Yanick Lefebvre


请发表答案对小组来说所有人都可以受益


Hi there! :O)

I need to replace all the accentued character of a string by it''s
non-accentued-character equivalent.

1. is there a way to do so without iterating trought the entire string and
replacing character, with the Convert or Encoding class for example?
2. if not, how can you actually replace a specific character in a string
without recreating a new string?

for now I do something like..
//***
string s = "aaaa ccaaa c aaa ca aaa";
char[] chars = s.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (chars[i] == ''c'')
{
chars[i] = ''x'';
}
s = new string(chars);
}
//***

this is seems to be the lighter way we''ve found to do it... :O/

I would done something like this but it''s not possible instead but the
compiler is complaining about the indexer being read-only..... (wich is
poor..)
//***
string s = "aaaa ccaaa c aaa ca aaa";

for (int i = 0; i < s.Lenght; i++)
{
if (s[i] == ''c'')
{
s[i] = ''x'';
}
}
//***

and I am **not** looking for the Replace() function...

thanks for the help..

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit

解决方案

Why don''t you want the Replace function? It was created so that you wouldn''t
have to write loops like you have there.

And if you must use the loop, why are you recreated the string at the end of
every loop? What is the point? You are always using just the last one, so
recreate the string from the char array once the loop is finished, no reason
to do it 20 times for 20 characters.
"Zoury" <yanick_lefebvre at hotmail dot com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...

Hi there! :O)

I need to replace all the accentued character of a string by it''s
non-accentued-character equivalent.

1. is there a way to do so without iterating trought the entire string and
replacing character, with the Convert or Encoding class for example?
2. if not, how can you actually replace a specific character in a string
without recreating a new string?

for now I do something like..
//***
string s = "aaaa ccaaa c aaa ca aaa";
char[] chars = s.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (chars[i] == ''c'')
{
chars[i] = ''x'';
}
s = new string(chars);
}
//***

this is seems to be the lighter way we''ve found to do it... :O/

I would done something like this but it''s not possible instead but the
compiler is complaining about the indexer being read-only..... (wich is
poor..)
//***
string s = "aaaa ccaaa c aaa ca aaa";

for (int i = 0; i < s.Lenght; i++)
{
if (s[i] == ''c'')
{
s[i] = ''x'';
}
}
//***

and I am **not** looking for the Replace() function...

thanks for the help..

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit



Zoury
Strings are immutable. If you want to replace characters in a string, then a new string must be created

Tu-Thac

----- Zoury wrote: ----

Hi there! :O

I need to replace all the accentued character of a string by it''
non-accentued-character equivalent

1. is there a way to do so without iterating trought the entire string an
replacing character, with the Convert or Encoding class for example
2. if not, how can you actually replace a specific character in a strin
without recreating a new string

for now I do something like.
//**
string s = "aaaa ccaaa c aaa ca aaa"
char[] chars = s.ToCharArray()
for (int i = 0; i < chars.Length; i++

if (chars[i] == ''c''

chars[i] = ''x''

s = new string(chars)

//**

this is seems to be the lighter way we''ve found to do it... :O

I would done something like this but it''s not possible instead but th
compiler is complaining about the indexer being read-only..... (wich i
poor..
//**
string s = "aaaa ccaaa c aaa ca aaa"

for (int i = 0; i < s.Lenght; i++

if (s[i] == ''c''

s[i] = ''x''
//**

and I am **not** looking for the Replace() function..

thanks for the help.

--
Best Regard
Yanick Lefebvr

Please posts answers to the group so all can benefi


> Strings are immutable. If you want to replace characters in a string,
then a new string must be created.

hhmmm... :O/
... thanks for the info.. :O)

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit


这篇关于字符串 - 优化问题..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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