English 中文(简体)
替换字符串中的字符
原标题:Replacing characters in a string

I have created a simple program that i want to replace some chars with others in array of string.I have created an array of string that contains some words,i want to loop on each word and check on some chars if it contains,but when i tried to replace nothing happens here is my code

            string x = "";
            x = "Script friends above about type=text/javascript>BBC.adverts.writeleaderboardtrue) all </script>friends,eating,khaled,khaled,khaled";
 char[] delimiterChars = {    ,  , ,  . ,  : ,  	 ,  : ,  $ ,  = ,  ; ,  < ,  > ,  ! ,  ; ,  ] ,  [ ,  " ,
                                     / , = , - , ? };
 string[] words = x.Split(delimiterChars);
            for (int j = 0; j < words.Length; j++)
            {
                words[j] = words[j].ToLower();
            }
 for (int i = 0; i < words.Length; i++)
            {
                for (int j = 0; j < words[j].Length; j++)
                {

                    if (words[i][j]== a ||words[i][j]== e ||words[i][j]== i ||words[i][j]== o ||words[i][j]== u 
                        ||words[i][j]== h ||words[i][j]== w ||words[i][j]== y )
                    {
                        words[i].Replace(words[i][j], 0 );
                    }
                    else if (words[i][j] ==  b  || words[i][j] ==  f  || words[i][j] ==  p  || words[i][j] ==  v )
                    {
                        words[i].Replace(words[i][j],  1 );
                    }

                     else if (words[i][j] ==  c  || words[i][j] ==  g  || words[i][j] ==  j  || words[i][j] ==  k 
                    || words[i][j] ==  q  || words[i][j] ==  s  || words[i][j] ==  x  || words[i][j] ==  z )
                {
                    char xx = words[i][j];
                    words[i].Replace(xx,  2 );
                    Console.WriteLine(words[i]);
                }
                else if (words[i][j] ==  d  || words[i][j] ==  t )
                {
                    words[i].Replace(words[i][j],  3 );
                }
                else if (words[i][j] ==  l )
                {
                    words[i].Replace(words[i][j],  4 );
                }
                else if (words[i][j] ==  m  || words[i][j] ==  n )
                {
                    words[i].Replace(words[i][j],  5 );
                }
                else if (words[i][j] ==  r )
                {
                    words[i].Replace(words[i][j],  6 );
                }



}
}
最佳回答

你忘了字符串是不可改变的

words[i]. replace(words[i][j], 1) 返回含有被替换值的新字符串。

您必须将引用重新指定到新生成的字符串对象 :

words[i] = words[i].Replace(words[i][j],  1 );
问题回答

. replace 操作不改变原始字符串。 您必须将结果放回源变量以应用更改 。

words[i] = words[i].Replace(words[i][j],  0 );

注意,替换适用于整个字符串,因此搜索每个单 a , e 等。 用字母替换字母和调用替换是多余的。 您可以这样做 :

words[i] = words[i].Replace( a ,  0 );
words[i] = words[i].Replace( e ,  0 );
// etc..

或更好,使用正则表达式:

words[i] = Regex.Replace(words[i], "[aeiouhwy]", "0");
words[i] = Regex.Replace(words[i], "[bfvp]", "1");

您可以使用 < a href=" "http://msdn.microsoft.com/es-es/library/xwewhkd1%28v=vs. 80%29.aspx" rel = "nofollow" >Regex. replace 来以更紧凑和高效的方式使同样的东西匹配。 I.e:

            for (int j = 0; j < words[j].Length; j++)
            {

                if (words[i][j]== a ||words[i][j]== e ||words[i][j]== i ||words[i][j]== o ||words[i][j]== u 
                    ||words[i][j]== h ||words[i][j]== w ||words[i][j]== y )
                {
                    words[i].Replace(words[i][j], 0 );
                }

可用简单的 Regex 替换 :

  Regex re0 = new Regex("[aeiouhwy]"); // match any of this chras
  string changedWord = re0.Replace(word,"0");

您甚至可以连线呼叫 replace ethods like this:

  Regex re0 = new Regex("[aeiouhwy]"); // match any of this chras
  Regex re1 = new Regex("[bfpv]");
  string changedWord = re0.Replace(word,"0");
  changedWord = re1.Replace(changedWord,"1")

写作和理解更容易,效率更高。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签