string.replace不能正常工作,使for循环变得无限。 [英] string.replace is not working fine, making the for loop to become infinite.

查看:251
本文介绍了string.replace不能正常工作,使for循环变得无限。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  string  str =  @ :?*); 

char [] invalidCharacters = Path.GetInvalidFileNameChars();

for (;;)
{
int index = str.IndexOfAny(invalidCharacters);
if (index < 0
break ;

char c4 = str [index];
string replace = string .Format( @ {0:x4},( ushort )c4);
str = str.Replace(c.ToString(),replace);
}





for循环变得无限。

解决方案

< blockquote>取决于 - 你是否在循环外定义了可变的 c ?您的意思是使用 c4 吗?

  char  c4 = str [index]; 
string replace = string .Format( @ {0:x4},( ushort )c4);
str = str.Replace(c.ToString(),replace);

成为:

  char  c4 = str [index]; 
string replace = string .Format( @ {0:x4},( ushort )c4);
str = str.Replace(c4.ToString(),replace);


您必须在以下行中进行更改。

而不是c使用c4



 str = str.Replace(c4.ToString(),replace); 


您好Srikanth,

您应该使用正则表达式替换方法一次替换多个实例。



参见简单示例:

 使用系统; 
使用 System.Text.RegularExpressions;

public class 示例
{
public static void Main()
{
string input = 这是文字太多了 +
whitespace。;
string pattern = \\ S +;
string replacement = ;
Regex rgx = new 正则表达式(模式);
string result = rgx.Replace(input,replacement);

Console.WriteLine( 原始字符串:{0},输入);
Console.WriteLine( 替换字符串:{0},结果);
}
}
// 该示例显示以下输出:
// 原始字符串:这是包含太多空格的文本。
// 替换字符串:这是包含太多空格的文本。



干杯,

Edo


string str = @":*?)";

            char[] invalidCharacters = Path.GetInvalidFileNameChars();

            for (;;)
            {
                int index = str.IndexOfAny(invalidCharacters);
                if (index < 0)
                    break;

                char c4 = str[index];
                string replace = string.Format(@"{0:x4}", (ushort)c4);
                str = str.Replace(c.ToString(), replace);
            }



The for loop is becoming infinite.

解决方案

Depends - did you define the varaiable c outside the loop? Did you mean to use c4 instead?

char c4 = str[index];
string replace = string.Format(@"{0:x4}", (ushort)c4);
str = str.Replace(c.ToString(), replace);

Becomes:

char c4 = str[index];
string replace = string.Format(@"{0:x4}", (ushort)c4);
str = str.Replace(c4.ToString(), replace);


You have to make changes in the following line.
Instead of c use c4

str = str.Replace(c4.ToString(), replace);


Hi Srikanth,
You should use regex Replace method to replace many instances at once.

See simple example:

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is   text with   far  too   much   " +
                     "whitespace.";
      string pattern = "\\s+";
      string replacement = " ";
      Regex rgx = new Regex(pattern);
      string result = rgx.Replace(input, replacement);

      Console.WriteLine("Original String: {0}", input);
      Console.WriteLine("Replacement String: {0}", result);
   }
}
// The example displays the following output:
//       Original String: This is   text with   far  too   much   whitespace.
//       Replacement String: This is text with far too much whitespace.


Cheers,
Edo


这篇关于string.replace不能正常工作,使for循环变得无限。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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