简体   繁体   中英

Fastest way to find first uppercase character in a string

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.

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..

In order to find the first uppercase character, I would use the PREG_OFFSET_CAPTURE flag of preg_match :

$string = "hiThere";

preg_match( '/[A-Z]/', $string, $matches, PREG_OFFSET_CAPTURE );

print_r( $matches[0] );

Which returns the following:

Array ( [0] => T [1] => 2 )

You could wrap this logic up into a function and use it over and over:

function firstUC ( $subject ) {
  $n = preg_match( '/[A-Z]/', $subject, $matches, PREG_OFFSET_CAPTURE );
  return $n ? $matches[0] : false;
}

echo ( $res = firstUC( "upperCase" ) ) ? $res[1] : "Not found" ;
// Returns: 5

echo ( $res = firstUC( "nouppers!" ) ) ? $res[1] : "Not found" ;
// Returns: Not found

Other way of doing

$stringCamelCase = 'stringCamelCase';// hiThere

Way with preg_split():

$array      = preg_split('#([A-Z][^A-Z]*)#', $stringCamelCase, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
echo strlen($array[0]);// $array = ['string', 'Camel', 'Case']

Way with strpbrk():

$CamelCase = strpbrk($stringCamelCase, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
echo strpos($stringCamelCase, $CamelCase);

I'd imagine doing this:

strlen(preg_replace("/^([a-z]*)[A-Z].*$/", "$1", $str));

isThis => 2
largerWord => 6

使用strpos()

echo strpos($string, "T");

Had the darndest time with this function till I found the array is a multi-dimensional. Didn't see anyone's code mention this. To get the position of the first Capitalized letter I used :

<?php

$field = "i_select_Interior_Quote";
preg_match( '/[A-Z]/', $field, $m, PREG_OFFSET_CAPTURE );

$posi=$m[0][1];

echo '<br><br>'.$posi; // outputs 9

?>

Solution for az characters. Probably wont work unicode

 preg_match('/[A-Z]/', $str, $m);
 if(isset($m[1])) {
    echo 'First upper char is '.$m[1].' and located at '. strpos($string, "T");
 }
 else {
    'There is no upperchar';
 }

Here's a (more or less) one line solution.

$pos = strcspn($string, 'ABCDEFGHJIJKLMNOPQRSTUVWXYZ');

strcspn will return the position of the first occurrence of any character in the second argument, or the length of the string if no character occurs.

Returns the length of the initial segment of str1 which does not contain any of the characters in str2.

$pos = strcspn($string, 'ABCDEFGHJIJKLMNOPQRSTUVWXYZ');
if ($pos < strlen($string)) {
    echo "capital letter as index $pos";
}

What I have used in my own application:

function acronyms($s){
  $s=str_replace(array('& ','of ','the ','and '),'',strtolower($s));
  $s=ucwords(trim($s)); //Collecting data
  if(strlen($s)>0 && preg_match_all('/[A-Z]/',$s,$m))   return implode('',$m[0]);
return $s;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM