String.Replace Anomoly [英] String.Replace Anomoly

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

问题描述

通常,我对String.Replace()没有任何问题。但是,我发现我需要更换角色的多个实例

a (\ xAAA)带有#符号。输入文件是一行简单的

文件。我在文件中读入一个名为strLine的字符串。然后,当我用
做一个简单的替换...这是我尝试过的:


strLine = strLine.Replace(" a"," #"); //不替换......


strLine = strLine.Replace(@a,#); //不替换......


strLine = strLine.Replace(@" \ xAA","#"); //不能替换...


if(strLine.Contains(@" \ xAA"))MessageBox.Show(" found one"); //否

消息框...


if(strLine.Contains(" a"))MessageBox.Show(" found one") ; //没有消息

box ...

if(strLine.Contains(@" a"))MessageBox.Show(" found one" ); //没有

留言框...


任何想法要么是我做错了,要么是更好的方法来尝试

替换是持久的角色,只是不会消失?

解决方案

Levidikus写道:
< blockquote class =post_quotes>
通常,我对String.Replace()没有任何问题。但是,我发现我需要更换角色的多个实例

a (\ xAAA)带有#符号。输入文件是一行简单的

文件。我在文件中读入一个名为strLine的字符串。然后,当我用
做一个简单的替换...这是我尝试过的:


strLine = strLine.Replace(" a"," #"); //不替换......


strLine = strLine.Replace(@a,#); //不替换......


strLine = strLine.Replace(@" \ xAA","#"); //不能替换...


if(strLine.Contains(@" \ xAA"))MessageBox.Show(" found one"); //否

消息框...


if(strLine.Contains(" a"))MessageBox.Show(" found one") ; //没有消息

box ...

if(strLine.Contains(@" a"))MessageBox.Show(" found one" ); //没有

留言框...


任何想法要么是我做错了,要么是更好的方法来尝试

替换是持久的角色,只是不会消失?



我认为你已经尝试了所有可能的组合,除了一个

有效...试试这个:


strLine = strLine.Replace(" \ xAA","#");

-

G?跑Andersson

_____
http://www.guffa.com


看起来你的strLine有问题。


以下按预期工作


string test =" \ xAA a" ;;

MessageBox.Show(

String.Concat(test,Environment.NewLine,

test.Replace(" a","#"),

Environment.NewLine ,

test.Replace(" \ xAA","#")));




9月18日下午2:51,Levidikus< james.h ... @ doveq.netwrote:


发现我需要替换该角色的多个实例

a (\ xAAA)带有#符号。输入文件是简单的一行



以下适用于我:


string feminineIndicatorChar = Char.ConvertFromUtf32(0xaa );

string b = feminineIndicatorChar.Replace(feminineIndicatorCha r,

"#"); // b =="#"


你确定你的strLine确实包含女性序数

指标吗?你能检查调试器吗?


Normally, I never have any problems with String.Replace(). However, I
found that I need to replace multiple instances of the character
"a" (\xAA) with a # symbol. The input file is a simple one line
file. I read in the file into a string called strLine. Then when I
do a a simple replace ... here is what I have tried:

strLine = strLine.Replace("a", "#"); // Doesn''t replace ...

strLine = strLine.Replace(@"a", "#"); // Doesn''t replace ...

strLine = strLine.Replace(@"\xAA", "#"); // Doesn''t replace ...

if (strLine.Contains(@"\xAA")) MessageBox.Show("found one"); // No
message box ...

if (strLine.Contains("a")) MessageBox.Show("found one"); // No message
box ...

if (strLine.Contains(@"a")) MessageBox.Show("found one"); // No
message box ...

Any ideas either what I''m doing wrong, or a better way to try to
replace is persistent character that just won''t go away?

解决方案

Levidikus wrote:

Normally, I never have any problems with String.Replace(). However, I
found that I need to replace multiple instances of the character
"a" (\xAA) with a # symbol. The input file is a simple one line
file. I read in the file into a string called strLine. Then when I
do a a simple replace ... here is what I have tried:

strLine = strLine.Replace("a", "#"); // Doesn''t replace ...

strLine = strLine.Replace(@"a", "#"); // Doesn''t replace ...

strLine = strLine.Replace(@"\xAA", "#"); // Doesn''t replace ...

if (strLine.Contains(@"\xAA")) MessageBox.Show("found one"); // No
message box ...

if (strLine.Contains("a")) MessageBox.Show("found one"); // No message
box ...

if (strLine.Contains(@"a")) MessageBox.Show("found one"); // No
message box ...

Any ideas either what I''m doing wrong, or a better way to try to
replace is persistent character that just won''t go away?

I think that you have tried every possible combination except the one
that works... Try this:

strLine = strLine.Replace("\xAA", "#");

--
G?ran Andersson
_____
http://www.guffa.com


Look like there is a problem with your strLine.

Following works as expected

string test = "\xAA a";
MessageBox.Show(
String.Concat(test,Environment.NewLine,
test.Replace("a", "#"),
Environment.NewLine,
test.Replace("\xAA", "#")));



On Sep 18, 2:51 pm, Levidikus <james.h...@doveq.netwrote:

found that I need to replace multiple instances of the character
"a" (\xAA) with a # symbol. The input file is a simple one line

The following works for me:

string feminineIndicatorChar = Char.ConvertFromUtf32(0xaa);
string b = feminineIndicatorChar.Replace(feminineIndicatorCha r,
"#"); // b == "#"

Are you sure your strLine really contains the feminine ordinal
indicator? Can you check in a debugger?


这篇关于String.Replace Anomoly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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