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

后端 未结 7 554
遇见更好的自我
遇见更好的自我 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 01:55

    How about

    string xx = "The quick brown fox jumps over the lazy dog";
    
    char search = 'f';
    
    var result = xx.Select((b, i) => b.Equals(search) ? i : -1).Where(i => i != -1);
    

提交回复
热议问题