简体   繁体   English

查找字符串中第一个大写字符的最快方法

[英]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) (本例中为T

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(如果只有1个匹配)或preg_match_all(如果你想要所有的匹配) 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 : 为了找到第一个大写字符,我将使用preg_matchPREG_OFFSET_CAPTURE标志:

$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(): 与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(): 用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 isThis => 2
largerWord => 6 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. az字符的解决方案。 Probably wont work unicode 可能不会工作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. strcspn将返回第二个参数中任何字符第一次出现的位置,如果没有字符,则返回字符串的长度。

Returns the length of the initial segment of str1 which does not contain any of the characters in str2. 返回str1的初始段的长度,该段不包含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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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