C#溶出/转换一个或多个字符 [英] C# Stripping / converting one or more characters

查看:147
本文介绍了C#溶出/转换一个或多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种快速的方法(而不必在一个字符串,通过每个字符明确循环),要么剥离或保持它。在Visual FoxPro,有,做它的伟大功能CHRTRAN()。其上的1:1的字符替换,但是如果在交替位置没有字符,其从最后的字符串剥离。前

Is there a fast way (without having to explicitly looping through each character in a string) and either stripping or keeping it. In Visual FoxPro, there is a function CHRTRAN() that does it great. Its on a 1:1 character replacement, but if no character in the alternate position, its stripped from the final string. Ex

CHRTRAN(这将是一个考验,它,X)

CHRTRAN( "This will be a test", "it", "X" )

会返回

ThXs wXll是ES

"ThXs wXll be a es"

注意,原来的我被转换成X ,和小写字母T是剥离出来。

Notice the original "i" is converted to "X", and lower case "t" is stripped out.

我看着替代类似的意图,但没有看到一个选项,什么也没有更换。

I looked at the replace for similar intent, but did not see an option to replace with nothing.

我在找做一些通用的程序来验证数据的多个来源具有不同类型的输入限制。一些数据可能是从外部源的到来,所以它不只是文本录入验证我需要测试。

I'm looking to make some generic routines to validate multiple origins of data that have different types of input restrictions. Some of the data may be coming from external sources, so its not just textbox entry validation I need to test for.

感谢

推荐答案

这是我最后的功能和完美的​​作品如预期。

Here was my final function and works perfectly as expected.

public static String ChrTran(String ToBeCleaned, 
                             String ChangeThese, 
                             String IntoThese)
{
   String CurRepl = String.Empty;
   for (int lnI = 0; lnI < ChangeThese.Length; lnI++)
   {
      if (lnI < IntoThese.Length)
         CurRepl = IntoThese.Substring(lnI, 1);
      else
         CurRepl = String.Empty;

      ToBeCleaned = ToBeCleaned.Replace(ChangeThese.Substring(lnI, 1), CurRepl);
   }
   return ToBeCleaned;
}

这篇关于C#溶出/转换一个或多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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