[英]How to split the same string depending on its characters?
I want to be able to create an array of characters as strings, from a given string.我希望能够从给定的字符串创建一个字符数组作为字符串。 Characters between () should be 1 element, and each character outside the () should be counted likewise.
()之间的字符应该是 1 个元素,并且()之外的每个字符都应该同样计算。 So a string like:
所以像这样的字符串:
string test = "(af)sd(abc)abc"
would generate an array that look like this:将生成一个如下所示的数组:
string[] testSplitted = { "af", "s", "d", "abc", "a", "b", "c" };
The code below is only able to make the characters inside the () items of the array but not the characters outside the () .下面的代码只能使数组的()项内的字符而不是()之外的字符。 So instead I get:
所以我得到:
string[] testSplitted = { "af", "sd", "abc", "abc" };
public static string[] transmissions = { "(ab)(bc)(ca)", "abc", "(abc)(abc)(abc)", "(zyx)bc" };
public static string[] words = { "abc", "bca", "dac", "dbc", "cba" };
public static void Main()
{
for (int y = 0; y < N; y++)
{
string[] charSplit = transmissions[y].Replace("(", "").Replace(")", "").Split();
int K = 0;
foreach (var item in words)
{
int matches = 0;
for (int i = 0; i < item.Length; i++)
{
if (!charSplit[i].Contains(item[i])) { break; }
matches++;
}
if (matches == 3) { K++; }
}
Console.WriteLine("Case #" + (y + 1) + ": " + K);
}
}
Is there anyway I can split the characters outside of the () to separate elements aswell?无论如何我可以将()之外的字符拆分为单独的元素吗?
Here's a pretty simple state machine that's O(n) .这是一个非常简单的 state 机器,它是O(n) 。
public static IEnumerable<string> StringWalker(string input)
{
bool inParens = false;
StringBuilder buffer = new StringBuilder();
List<string> result = new List<string>();
foreach (var c in input)
{
if (c == '(')
{
if (inParens)
{
throw new Exception("Nested Parens not allowed");
}
inParens = true;
buffer.Clear();
}
else if (inParens)
{
if (c == ')')
{
inParens = false;
result.Add(buffer.ToString());
}
else
{
buffer.Append(c);
}
}
else
{
result.Add(c.ToString());
}
}
return result;
}
The results look like:结果如下所示:
abc(de)(fgh)ijk: ==> (a) (b) (c) (de) (fgh) (i) (j) (k)
(abc)de(f)gh(ijklm)nop: ==> (abc) (d) (e) (f) (g) (h) (ijklm) (n) (o) (p)
You can try using reguar expressions and Match
all the items:您可以尝试使用正则表达式并
Match
所有项目:
using System.Linq;
using System.Text.RegularExpressions;
...
string test = "(af)sd(abc)abc";
string[] testSplitted = Regex
.Matches(test, @"\(.*?\)|.")
.Cast<Match>()
.Select(match => match.Value.Trim('(', ')'))
.ToArray();
More tests:更多测试:
string[] tests = new string[] {
"(ab)(bc)(ca)",
"abc",
"(abc)(abc)(abc)", "(zyx)bc"
};
static string[] Perform(string value) => Regex
.Matches(value, @"\(.*?\)|.")
.Cast<Match>()
.Select(match => match.Value.Trim('(', ')'))
.ToArray();
string report = string.Join(Environment.NewLine, tests
.Select(test => $"{test,15} => {string.Join(", ", Perform(test))}"));
Console.Write(report);
Output: Output:
(ab)(bc)(ca) => ab, bc, ca
abc => a, b, c
(abc)(abc)(abc) => abc, abc, abc
(zyx)bc => zyx, b, c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.