我想检查包含某些字符串的文件(用#
分隔)是否包含双重重复符号。例:
我有这样一个文件:
1234#224859#123567
我正在读取此文件并将用#
分隔的字符串放入数组中。
我想找到哪些字符串有一个重复的数字(在这种情况下是224859
)并返回在该字符串中重复的第一个数字的位置?
这是我到目前为止所做的:
ArrayList list = new ArrayList();
OpenFileDialog openFile1 = new OpenFileDialog();
int size = -1;
DialogResult dr = openFile1.ShowDialog();
string file = openFile1.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
string temp = "";
for (int i = 0; i < text.Length; i++)
{
if (text[i] != '#')
{
temp += text[i].ToString();
}
else
{
list.Add(temp);
temp = "";
}
}
}
catch (IOException)
{
}
string all_values = "";
foreach (Object obj in list)
{
all_values += obj.ToString() + " => ";
Console.WriteLine(" => ", obj);
}
textBox1.Text = (all_values);
答案 0 :(得分:3)
这个正则表达式应该可以解决问题。
var subject = "1234#224859#123567";
foreach(var item in subject.Split('#'))
{
var regex = new Regex(@"(?<grp>\d)\k<grp>");
var match =regex.Match(item);
if(match.Success)
{
Console.WriteLine("Index : {0}, Item:{1}", match.Index, item);
//prints Index : 0, Item:224859
}
}
答案 1 :(得分:1)
这是一种比Sriram更具程序性的方法,但主要的好处是记住你的结果,以便稍后在你的程序中使用它们。
基本上,字符串是基于#
分隔符拆分的,它会返回一个string[]
,其中包含每个数字。然后,对于每个字符串,您遍历字符并检查i
处的当前字符是否与i + 1
处的下一个字符匹配。如果是这样,重复数字的最早出现位于i
,因此会记住i
,并且我们会突破处理char
的循环。
由于int
是非可空类型,我决定使用-1
来表示在字符串中找不到匹配项。
Dictionary<string, int> results = new Dictionary<string, int>();
string text = "1234#224859#123567#11#4322#43#155";
string[] list = text.Split('#');
foreach (string s in list)
{
int tempResult = -1;
for (int i = 0; i < s.Length - 1; i++)
{
if(s.ElementAt(i) == s.ElementAt(i + 1))
{
tempResult = i;
break;
}
}
results.Add(s, tempResult);
}
foreach (KeyValuePair<string, int> pair in results)
{
Console.WriteLine(pair.Key + ": " + pair.Value);
}
输出:
1234:-1
224859:0
123567:-1
11:0
4322:2
43:-1
155:1
答案 2 :(得分:0)
这会做你想要的吗?
string text = File.ReadAllText(file);
string[] list = text.Split(new char[] { '#' });
然后,将字符串分开后:
foreach (string s in list)
{
int pos = HasDoubleCharacter(s);
if (pos > -1)
{
// do something
}
}
private static int HasDoubleCharacter(string text)
{
int pos = 0;
char[] c3 = text.ToCharArray();
char lastChar = (char)0;
foreach (char c in c3)
{
if (lastChar == c)
return pos;
lastChar = c;
pos++;
}
return -1;
}
或者您只是在寻找原始文本中所有双打的位置列表。如果是这样(你不需要分别对各种字符串采取行动,你可以试试这个:
private static List<int> FindAllDoublePositions(string text)
{
List<int> positions = new List<int>();
char[] ca = text.ToCharArray();
char lastChar = (char)0;
for (int pos = 0; pos < ca.Length; pos++)
{
if (Char.IsNumber(ca[pos]) && lastChar == ca[pos])
positions.Add(pos);
lastChar = ca[pos];
}
return positions;
}
答案 3 :(得分:0)
这是另一个有效的正则表达式
int indexof = -1;
String input = "3492883#32280948093284#990303294";
string[] numbers = input.Split('#');
foreach(string n in numbers)
{
Match m=Regex.Match(n, @"(\d)\1+");
if (m.Success)
{
indexof = m.Index;
}
}
答案 4 :(得分:-1)
如果您正在寻找特定的字符串模式,Regex最有可能成为您最好的朋友:
string text = "1234#224859#123567asdashjehqwjk4234#244859#123567asdhajksdhqjkw1434#244859#123567";
var results = Regex.Matches(text, @"\d{4}#(?<Value>\d{6})#\d{4}");
var allValues = "";
foreach (Match result in results)
{
allValues = result.Groups["Value"].Value + " => ";
Console.WriteLine(" => ", result.Value);
}