其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

检查字符串是否是正则表达式的字母数字

GG网络技术分享 2025-03-18 16:15 53


问题描述:

I want to check if a string is alpha-numeric and don\'t intend to use Regex. I have am getting the correct answer but somehow the program is throwing an error stating undefined offset. I have checked the array keys and seemingly they are fine.

$str=\"hello\";

$arr=str_split($str);//convert a string to an array

$a=0;

$d=0;

for($i=0;$i<=count($arr);$i++)

{

if($arr[$i]>=\'a\' && $arr[$i]<=\'z\' || $arr[$i]>=\'A\' && $arr[$i]<=\'Z\')

{

$a=1;

}

elseif($arr[$i]>=\'0\' && $arr[$i]<=\'9\')

{

$d=1;

}

}

if($a==1&&$d==1)

{

echo \"Alphanumeric\";

}

else

{

echo \"Not alphanumeric\";

}

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我想检查一个字符串是否为字母数字,并且不打算使用 Regex </ 代码>。 我得到了正确的答案,但不知何故该程序抛出一个错误,说明 undefined offset </ code>。 我检查了数组键,看起来很好。 </ p>

  $ str =“hello”; 

$ arr = str_split($ str); //将字符串转换为数组

$ a = 0;

$ d = 0;

for($ i = 0; $ i&lt; = count($ arr); $ i ++)

{

if if($ arr [$ i]&gt; =\'a\'&amp;&amp; $ arr [$ i]&lt; =\'z\'|| $ arr [$ i]&gt; =\'A\'&amp;&amp; $ arr [$ i]&lt; =\'Z\')

{

$ a = 1;

}

elseif($ arr [$ i]&gt; =\'0\'&amp;&amp; $ arr [$ i]&lt; =\'9\')

{

$ d = 1; \\ n}

}

nif($ a == 1&amp;&amp; $ d == 1)

{

echo“Alphanumeric”;

}

else

{

echo“not alphanumeric”;

}

</ code> </ pre>

</ div>

网友观点:

Array start at index zero, so the end ist i<count

for($i=0;$i<count($arr);$i++)

You should check out the ctype_alnum ( string $text ); function.

ctype_alnum

$str=\\\"hello\\\";

if(ctype_alnum($str))

{

echo \\\"Alphanumeric\\\";

}

else

{

echo \\\"Not alphanumeric\\\";

}

You should use either $i < count($arr) or $i <= count($arr) -1 as arrays start at 0 and having just $i <= count($arr) will result in an undefined offset error message.

如何在 Python 中检查字符串是否包含数字?

如果给定的字符串包含数字,则 Python 内置的 any 函数与 str.isdigit 一起将返回 True。否则返回 False。

如果给定的字符串包含数字,则模式为 r'\\d'的 Python 正则表达式搜索方法也可以返回 True。

1.带有 str.isdigit 的 Python any 函数来检查字符串是否包含数字

如果给定的 Python 迭代对象的任何元素为True,则any函数返回True,否则,返回False

如果给定字符串中的所有字符均为数字,则 str.isdigit() 返回 True,否则返回 False

标签:

提交需求或反馈

Demand feedback