C#剥离/转换一个或多个字符
有没有一种快速的方法(无需显式循环遍历字符串中的每个字符)并剥离或保留它。 在Visual FoxPro中,有一个functionCHRTRAN(),它做得很好。 它以1:1字符替换,但如果在替换位置没有字符,则将其从最终字符串中剥离。 防爆
CHRTRAN(“这将是一个测试”,“它”,“X”)
将返回
“ThXs将成为一个es”
注意原始“i”被转换为“X”,小写“t”被删除。
我看了替换类似的意图,但没有看到一个替换什么都没有的选项。
我正在寻找一些通用例程来validation具有不同类型输入限制的多个数据源。 一些数据可能来自外部来源,因此我不仅需要测试文本框条目validation。
谢谢
您只需要对String.Replace()进行几次调用即可。
string s = "This will be a test"; s = s.Replace("i", "X"); s = s.Replace("t", "");
请注意,Replace() 返回一个新字符串。 它不会改变字符串本身。
这是你想要的吗?
"This will be a test".Replace("i", "X").Replace("t", String.Empty)
这是CHRTRAN
函数的一个简单实现 – 如果字符串包含\0
并且非常混乱,则它不起作用。 你可以用循环编写一个更好的,但我只是想用LINQ来尝试。
public static String ChrTran(String input, String source, String destination) { return source.Aggregate( input, (current, symbol) => current.Replace( symbol, destination.ElementAtOrDefault(source.IndexOf(symbol))), preResult => preResult.Replace("\0", String.Empty)); }
你可以使用它。
// Returns "ThXs wXll be a es" String output = ChrTran("This will be a test", "it", "X");
只是为了得到一个干净的解决方案 – 没有LINQ也一样,并且也适用于\0
情况,并且它几乎到位,因为使用了StringBuilder
但当然不会修改输入。
public static String ChrTran(String input, String source, String destination) { StringBuilder result = new StringBuilder(input); Int32 minLength = Math.Min(source.Length, destination.Length); for (Int32 i = 0; i < minLength; i++) { result.Replace(source[i], destination[i]); } for (Int32 i = minLength; i < searchPattern.Length; i++) { result.Replace(source[i].ToString(), String.Empty); } return result.ToString(); }
缺少空引用处理。
受到tvanfosson解决方案的启发,我给了LINQ第二枪。
public static String ChrTran(String input, String source, String destination) { return new String(input. Where(symbol => !source.Contains(symbol) || source.IndexOf(symbol) < destination.Length). Select(symbol => source.Contains(symbol) ? destination[source.IndexOf(symbol)] : symbol). ToArray()); }
这是我的最终function,并按预期完美运行。
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; }
要“替换为空”,只需用空字符串替换即可。 这会给你:
String str = "This will be a test"; str = str.Replace("i", "X"); str = str.Replace("t","");
作为字符串扩展名的更通用的版本。 与其他人一样,这不会进行转换,因为字符串在C#中是不可变的,而是返回一个带有指定替换的新字符串。
public static class StringExtensions { public static string Translate( this string source, string from, string to ) { if (string.IsNullOrEmpty( source ) || string.IsNullOrEmpty( from )) { return source; } return string.Join( "", source.ToCharArray() .Select( c => Translate( c, from, to ) ) .Where( c => c != null ) .ToArray() ); } private static string Translate( char c, string from, string to ) { int i = from != null ? from.IndexOf( c ) : -1; if (i >= 0) { return (to != null && to.Length > i) ? to[i].ToString() : null; } else { return c.ToString(); } } }
这是我认为使用LINQ使问题过于复杂的情况。 这很简单,重点:
private static string Translate(string input, string from, string to) { StringBuilder sb = new StringBuilder(); foreach (char ch in input) { int i = from.IndexOf(ch); if (from.IndexOf(ch) < 0) { sb.Append(ch); } else { if (i >= 0 && i < to.Length) { sb.Append(to[i]); } } } return sb.ToString(); }
public static string ChrTran(string cSearchIn,string cSearchFor,string cReplaceWith) { string result =“”; dynamic inArray = cSearchIn.ToCharArray(); foreach(inArray中的var caracter) { int position = cSearchFor.IndexOf(caracter); result = result + cReplaceWith.Substring(position,1);
} return result; }