本文介绍了更高效的方式来获得一个字符的所有指标在一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而不是通过每个字符循环,看它是否是一个那么你要添加索引你到一个列表,像这样的:

  VAR foundIndexes =新的List< INT>(); 
的for(int i = 0; I< myStr.Length;我++)
{
如果(myStr的[I] =='A')
foundIndexes.Add(我);
}


解决方案

您可以使用string.indexof见

 字符串s =abcabcabcabcabc的例子; 
变种foundIndexes =新的List< INT>();

长T1 = DateTime.Now.Ticks;
的for(int i = s.IndexOf('A');我-1个; I = s.IndexOf('A',我+ 1))
{
//循环结束时,我= -1('A'未找到)
foundIndexes.Add(I)
}
长T2 = DateTime.Now.Ticks - T1; //读取此值,以运行时间


Instead of looping through each character to see if it's the one you want then adding the index your on to a list like so:

     var foundIndexes = new List<int>();
     for (int i = 0; i < myStr.Length; i++)
     {
        if (myStr[i] == 'a')
           foundIndexes.Add(i);
     }
解决方案

You can use string.indexof, see example

    string s = "abcabcabcabcabc";
    var foundIndexes = new List<int>();

    long t1 = DateTime.Now.Ticks;
    for (int i = s.IndexOf('a'); i > -1; i = s.IndexOf('a', i + 1))
        {
         // for loop end when i=-1 ('a' not found)
                foundIndexes.Add(i);
        }
    long t2 = DateTime.Now.Ticks - t1; // read this value to see the run time

这篇关于更高效的方式来获得一个字符的所有指标在一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:02