More efficient way to get all indexes of a character in a string

后端 未结 7 547
遇见更好的自我
遇见更好的自我 2020-12-04 01:41

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&         


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 02:00

    The raw iteration is always better & most optimized.

    Unless it's a bit complex task, you never really need to seek for a better optimized solution...

    So I would suggest to continue with :

    var foundIndexes = new List();
    
    for (int i = 0; i < myStr.Length; i++)
    
         if (myStr[i] == 'a') foundIndexes.Add(i);
    

提交回复
热议问题