C#:用分隔符分割string,但在结果中保留分隔符
我想用分隔符分割一个string,但在结果中保留分隔符
我将如何在C#中做到这一点?
提前致谢!
如果你想让分隔符成为“自己的分割”,你可以使用Regex.Split例如:
string input = "plum-pear"; string pattern = "(-)"; string[] substrings = Regex.Split(input, pattern); // Split on hyphens foreach (string match in substrings) { Console.WriteLine("'{0}'", match); } // The method writes the following to the console: // 'plum' // '-' // 'pear'
这个版本不使用LINQ或正则expression式,所以它可能相对高效。 我认为它可能比正则expression式更容易使用,因为你不必担心转义特殊的分隔符。 它返回的IList<string>
比总是转换成数组更有效率。 这是一个扩展方法,很方便。 您可以将分隔符作为数组或多个parameter passing。
/// <summary> /// Splits the given string into a list of substrings, while outputting the splitting /// delimiters (each in its own string) as well. It's just like String.Split() except /// the delimiters are preserved. No empty strings are output.</summary> /// <param name="s">String to parse. Can be null or empty.</param> /// <param name="delimiters">The delimiting characters. Can be an empty array.</param> /// <returns></returns> public static IList<string> SplitAndKeepDelimiters(this string s, params char[] delimiters) { var parts = new List<string>(); if (!string.IsNullOrEmpty(s)) { int iFirst = 0; do { int iLast = s.IndexOfAny(delimiters, iFirst); if (iLast >= 0) { if (iLast > iFirst) parts.Add(s.Substring(iFirst, iLast - iFirst)); //part before the delimiter parts.Add(new string(s[iLast], 1));//the delimiter iFirst = iLast + 1; continue; } //No delimiters were found, but at least one character remains. Add the rest and stop. parts.Add(s.Substring(iFirst, s.Length - iFirst)); break; } while (iFirst < s.Length); } return parts; }
一些unit testing:
text = "[a link|http://www.google.com]"; result = text.SplitAndKeepDelimiters('[', '|', ']'); Assert.IsTrue(result.Count == 5); Assert.AreEqual(result[0], "["); Assert.AreEqual(result[1], "a link"); Assert.AreEqual(result[2], "|"); Assert.AreEqual(result[3], "http://www.google.com"); Assert.AreEqual(result[4], "]");
我会说最简单的方法来完成这个(Hans Kesting提出的参数除外)是以常规方式分割string,然后遍历数组并将分隔符添加到除最后一个之外的每个元素。
我想做这样的多行string,但需要保持断行,所以我做到了这一点
string x = @"line 1 {0} line 2 {1} "; foreach(var line in string.Format(x, "one", "two") .Split("\n") .Select(x => x.Contains('\r') ? x + '\n' : x) .AsEnumerable() ) { Console.Write(line); }
产量
line 1 one line 2 two
我遇到了同样的问题,但有多个分隔符。 这是我的解决scheme:
public static string[] SplitLeft(this string @this, char[] delimiters, int count) { var splits = new List<string>(); int next = -1; while (splits.Count + 1 < count && (next = @this.IndexOfAny(delimiters, next + 1)) >= 0) { splits.Add(@this.Substring(0, next)); @this = new string(@this.Skip(next).ToArray()); } splits.Add(@this); return splits.ToArray(); }
分离CamelCasevariables名称的示例:
var variableSplit = variableName.SplitLeft( Enumerable.Range('A', 26).Select(i => (char)i).ToArray());
为了避免添加字符到新行尝试这个:
string[] substrings = Regex.Split(input,@"(?<=[-])");