Detect if a string contains uppercase characters

前端 未结 7 916
孤城傲影
孤城傲影 2021-01-17 09:32

Is there an alternative to using a regular expression to detect if a string contains uppercase characters? Currently I\'m using the following regular expression:

<         


        
7条回答
  •  梦毁少年i
    2021-01-17 09:50

    using for loops, not as efficient and readable as the other methods pointed out, but for starters should work and provide a comprehensive way of doing this:

    int counter = 0;
    for(int i=0; i< myString.Length;i++)
        {
            //if character is upper add +1 to counter
            if(char.IsUpper(chaineNonPascale[i]))
            {
                counter++;
            }
        }
    

    Basically, you iterate over your string and check for Upper Chars, then you can add logic as to what to do with the place where there is an Upper Char. For example, insert a space where the second upper case char is found and then use the ToLower method on the whole string...

提交回复
热议问题