问题描述
我这里有一些代码,基本上在一个字符串中查找,
出现任何3个相同的字符串。所以这个函数会报告AAA
bbb等。我后来添加了一些代码
,这是检测相同char出现的情况所需的时间4
次,例如AAAA,在这种情况下想跳过剩下的3
chars并检查字符串中的下一个字符。这导致我在我的for循环中增加了我的for循环计数器,我知道这是
应该是不好的练习,你们有什么建议吗? br />
无论如何这里代码 - thx
private bool lookfor3CharacterOccurance(字符串行,字符串路径)
{
bool found = false;
for(int i = 0; i< = line.Length-4; i ++)
{
//对这些格式化字符不感兴趣
if(line [i]!=''''&&(line [i]! =''\ u000A'')&&(line [i]!=''\ u000B'')
&&(line [i]!=''\\ \\ u00C'')&&(line [i]!=''\ u000D'')&&(line [i]!=
''\ u2028'' )&&(line [i]!=''\ u2029'')&&(line [i]!=''\ u0009''))
{
//只有相同类型的3个字符的重复次数
if((line [i] == line [i + 1])& amp;&(line [i] == line [i + 2]))
{
if(!(line [i] == line [i + 3]))
{
//Console.WriteLine("{0} {1} {2}",line [i],line [i + 1],第[i + 2]行;
shhpErr.Add(" File:" +路径+包含至少一次出现的相同字符的次数为
:+ line [i] + line [i + 1] + line [i + 2]);
found = true;
break;
}
else
{
i = i + 3;
}
}
}
}
返回找到;
}
hi,
I have some code here which basically look for within a string, the
occurance of any 3 consectative characters which are the same. so AAA
bbb etc would be reported by this function. I later added some code
which is needed to detect the scenario where the same char appears 4
times for example AAAA, in this case want to skip the remaining 3
chars and check the next character in the string. This has led me to
increase my for loop counter within my for loop, I know this is
supposed to be bad practice, is there anything you guys can suggest?
Here the code anyway - thx
private bool lookfor3CharacterOccurance(string line, string path)
{
bool found = false;
for(int i=0;i<=line.Length-4;i++)
{
//not interested in these formatting characters
if(line[i] != '' '' && (line[i] != ''\u000A'') && (line[i] != ''\u000B'')
&& (line[i] != ''\u000C'') && (line[i] != ''\u000D'') && (line[i] !=
''\u2028'') && (line[i] != ''\u2029'') && (line[i] != ''\u0009''))
{
//only what reoccurances of 3 characters of the same type
if((line[i] == line[i+1] ) && (line[i] == line[i+2] ) )
{
if(!(line[i] == line[i+3]))
{
//Console.WriteLine("{0} {1} {2}",line[i],line[i+1],line[i+2]);
shhpErr.Add("File: " + path + " contains at least one occurances of
the same characters 3 times: "+line[i]+line[i+1]+line[i+2]);
found = true;
break;
}
else
{
i=i+3;
}
}
}
}
return found;
}
This is actually bad practice, because ''for'' corresponds to
an idiom in C (and C-based languages), which is a data structure
traversal (array, list, etc.): perform a given task for every
element in the data structure. You can read the following line:
for (int i = 0; i < array.length; i++) { ... }
as the C translation of "for every element in ''array'' do ...".
''for'' loops are sometimes wrongly used for situations that do not
correspond to that idiom. In that case, ''while'' should be used
instead:
int i = 0;
while (i < array.length) {
... // a task that may vary according to the contents
// of the array
i++;
}
--
Matthieu Villeneuve
这篇关于你怎么能改进这个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!