我正在尝试将国家/地区添加到Excel文档中,其中包含一些有关采用的信息。该信息在HERE在线。我一直在向后工作,所以我至少可以在接下来的几个小时内展示一些东西。如果你好奇,我已经在这个问题的底部添加了我真正想做的事情。
我发现问题与回车和换行有关。我正在努力修复,然后我会上传解决方案。
我正在传递一个大约450个单词的字符串。我有2'>>>'和2'<<'在里面。我的输出只显示我只找到1'>>>'。有什么我想念的吗?我准备开始拔头发了。
private void FindCountries(string text) {
text = text.Replace("\n", " "); //This 3 lines fixed the problem.
text = text.Replace("\r", " "); //Without these and string in the array was
text = text.Replace("\t", " "); //looking like this: "<<\r\n\r>>>"
string[] helper = text.Split(' ');
//List<string> myContries = new List<string>();
List<int> j = new List<int>();
List<int> k = new List<int>();
for (int i = 0; i < helper.Length; i++) {
if (helper[i] == ">>>") {
Console.WriteLine(helper[i]);
Console.WriteLine("added to j");
j.Add(i);
}
if (helper[i] == "<<") {
Console.WriteLine("added to k");
k.Add(i);
}
}
Console.WriteLine("J");
foreach (var item in j) {
Console.WriteLine(item);
}
Console.WriteLine("K");
foreach (var item in k) {
Console.WriteLine(item);
}
}
项目描述: 去一个网站。 获取所有信息。(现在我在这里工作。) 很好地把它放在Excel Doc中。
答案 0 :(得分:1)
输入字符串"foo << bar >>> baz >>> qux << quxx >>>"
的快速测试按预期工作。
在不知道输入是什么的情况下,我只能猜测,但有一种可能性是>>>
的出现之一没有被两边的空格分隔,这意味着它不会在你自己出现时出现拆分输入。
答案 1 :(得分:0)
private void FindCountries(string text)
{
var words = text.Split(' ');
//List<string> myContries = new List<string>();
var tripleRights = new List<int>();
var doubleLefts = new List<int>();
for (var index = 0; index < words.Length; index++)
{
if (String.Equals(words[index], ">>>", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine(words[index]);
Console.WriteLine("found a triple right");
tripleRights.Add(index);
}
if (String.Equals(words[index], "<<", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine(words[index]);
Console.WriteLine("found a double left");
doubleLefts.Add(index);
}
}
Console.WriteLine("triple rights:");
foreach (var item in tripleRights)
{
Console.WriteLine(item);
}
Console.WriteLine("double lefts");
foreach (var item in doubleLefts)
{
Console.WriteLine(item);
}
}
答案 2 :(得分:0)
您可以简单地执行以下操作:
string test = "23 <<< hello <<< to >> here <<<";
int found=0;
int seek_pos =0;
while (true) {
var current = test.IndexOf("<<<", seek_pos);
if (current == -1)
break;
else {
Console.WriteLine("found one at position {0}, starting at: {1} ", current, seek_pos);
found++;
seek_pos = current + 3 ; // 3 chars in <<<
}
}
Console.WriteLine("Total found: {0} ",found);
给出结果:
found one at position 3, starting at: 0
found one at position 13, starting at: 6
found one at position 28, starting at: 16
Total found: 3