Fastest way to find first uppercase character in a string

Martin picture Martin · Apr 13, 2012 · Viewed 16.5k times · Source

Suppose you have this string:

hiThere

What is the fastest way to find where the first upper case character is? (T in this example)

I am worried about performance as some words are quite long.

Answer

crazyphoton picture crazyphoton · Apr 13, 2012

Easiest would be to use preg_match (if there is only 1 match) or preg_match_all (if you want all the matches) http://php.net/manual/en/function.preg-match.php

preg_match_all('/[A-Z]/', $str, $matches, PREG_OFFSET_CAPTURE);

Not sure if it is the fastest..