Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
$i, потому выскакивает Noticefunction check_for_number($str)
{
$lenght = strlen($str);
for($i=0;$i<$lenght;)
{
if ((int)$str[$i++])
{
return true;
}
}
return false;
}
var_dump(check_for_number('test-111111')); // true
var_dump(check_for_number('test-000000')); // epic fail
function check_for_number($str) {
$i = strlen($str);
while ($i--) {
if (is_numeric($str[$i])) return true;
}
return false;
}
function hasDigit($str) {
$haystack = '0123456789';
for ($i = 0; $i <= 9; ++$i) {
if (strpos($str, $haystack[$i]) !== false) {
return true;
}
}
return false;
}def integer_in_string(str=''):
for x in range(10):
if x in str:
return True
return False
String.prototype.hasDigit = function () {
for (var i = this.length; i--;) {
if (!isNaN(this[i])) return true;
}
return false;
};
String.prototype.hasDigit = function () {
for (var i = this.length; i--;) {
if (!isNaN(this[i])) return true;
}
return false;
};
alert([
'hello-000-hello'.hasDigit() ? 1 : 0, // 1
'hello-111-hello'.hasDigit() ? 1 : 0, // 1
'hello-xxx-hello'.hasDigit() ? 1 : 0 // 0
]);
/\d/.test(this);!this.match(/\d/);String.prototype.hasDigit = function () {
return this.split('').filter(isNaN).join('').length == this.length;
};
String.prototype.hasDigit = function () {
return !!this.split('').filter(function(v) !isNaN(v)).length;
};
String.prototype.hasDigit = function () {
return this.split('').some(function(v) !isNaN(v));
};
String.prototype.hasDigit = function () {
return !this.split('').every(isNaN);
};
String.prototype.hasDigit = function(){
return !!Array.prototype.filter.call(this, function(){if(!isNaN(arguments[0])){return true}}).length
}
Array.prototype.filter.call можно было заменить на [].filter.callif(!isNaN(arguments[0])){return true} на return !isNaN(arguments[0]). Хотя лучше добавить аргумент в объявление и тогда получится такой код:String.prototype.hasDigit = function(){
return !![].filter.call(this, function(a){ return !isNaN(a) }).length
}
// Javascript 1.8:
String.prototype.hasDigit = function(){
return !![].filter.call(this, function(a) !isNaN(a) ).length
}
def has_digit(value):
return any(''.join([_ for _ in value if _.isdigit()]))
def has_digit(value): return value.isalpha()==value.isalnum()
def has_digit(value): return sorted(value)[0].isdigit()
def has_digit(value): return any((i in value) for i in '0123456789')
def has_digit(value): return any((i in value) for i in string.digits)
def has_digit(value): return any((i in value) for i in map(str,range(10)))
def has_digit(value): return value==value.strip(string.digits)
def has_digit(value): return len(value)==len(value.strip(string.digits))
const char *pointer; const char *source; const int current; for (pointer = source; (current = *pointer++) != 0;) if (table[current]) return true;
const char *pointer; const char *source; const char current; for (pointer = source; (current = *pointer++) != 0;) if (isalpha(char)) return true;
function check_for_number($str)
{
for ($i = strlen($str) - 1; $i >= 0 && ($str[$i] < '0' || $str[$i] > '9'); --$i);
return $i >= 0;
}check = any isDigit
function check_for_number($str, $range){
return ($str != str_replace($range, '', $str));
}
$test_string = 'test-0123456';
$range = range('0', '9');
var_dump(check_for_number($test_string, $range));
$test_string = 'test';
var_dump(check_for_number($test_string, $range));
function IsHaveDigit(str: string): Boolean;
var
i: Integer;
begin
Result := False;
for i := 1 to Length(str) do
if str[i] in ['0'..'9'] then
begin
Result := True;
Break;
end;
end;
bool hasDigit(string str)
{
return (str.IndexOfAny("1234567890".ToCharArray()) >= 0);
}
static bool HasDigit(String input)
{
return input.Any(c => Char.IsDigit©);
}
bool hasDigit(char* str)
{
int tmp[256];
for (char* pos = str; *pos; ++pos) ++tmp[*pos];
for (char i = '0'; i <= '9'; ++i) if (tmp[i]) return true;
return false;
}
bool hasDigit(char* str)
{
int tmp[256];
for (char i = '0'; i <= '9'; ++i) tmp[i] = 0;
for (char* pos = str; *pos; ++pos) ++tmp[*pos];
for (char i = '0'; i <= '9'; ++i) if (tmp[i]) return true;
return false;
}
borisko@vaio:~/test-num$ cat test.cpp#include <stdio.h>
#define LEN (1000 * 1000 * 100)
char line[LEN + 1];
bool hasDigit(char* str)
{
#ifdef MY_METHOD
int tmp[256];
for (char i = '0'; i <= '9'; ++i) tmp[i] = 0;
for (char *pos = str; *pos; ++pos) ++tmp[*pos];
for (char i = '0'; i <= '9'; ++i) if (tmp[i]) return true;
return false;
#else
for (char *pos = str; *pos; ++pos) if ('0' <= *pos && *pos <= '9') return true;
return false;
#endif
}
int main()
{
for(int i = 0; i < LEN; ++i) line[i] = 'a';
line[LEN] = 0;
hasDigit(line);
}
#include <stdio.h>
#include <time.h>
#define LEN (1000 * 1000 * 100)
char line[LEN + 1];
bool HasDigit1(char* str)
{
int tmp[256];
for (char i = '0'; i <= '9'; ++i) tmp[i] = 0;
for (char *pos = str; *pos; ++pos) ++tmp[*pos];
for (char i = '0'; i <= '9'; ++i) if (tmp[i]) return true;
return false;
}
bool HasDigit2(char *str){
for (char *pos = str; *pos; ++pos) if ('0' <= *pos && *pos <= '9') return true;
return false;
}
bool HasDigit3(char *str){
while(*str) if((unsigned int)(*str++-'0')<10) return true;
return false;
}
int main()
{
for(int i = 0; i < LEN; ++i) line[i] = 'a';
line[LEN] = 0;
int N=100;
int res=0;
long m=clock();
for(int i=0;i<N; i++) res+=HasDigit1(line) ? 1 : 0;
m=clock()-m;
printf("Method1: %lf, %d\n",(double)m/N/CLOCKS_PER_SEC,res);
m=clock(); res=0;
for(int i=0;i<N; i++) res+=HasDigit2(line) ? 1 : 0;
m=clock()-m;
printf("Method2: %lf, %d\n",(double)m/N/CLOCKS_PER_SEC,res);
m=clock(); res=0;
for(int i=0;i<N; i++) res+=HasDigit3(line) ? 1 : 0;
m=clock()-m;
printf("Method3: %lf, %d\n",(double)m/N/CLOCKS_PER_SEC,res);
scanf("%d",&N);
}
for(int i = 0; i < LEN; ++i){
char s=(char)i;
s^=(s>>7)&1;
if(s==0 || (s>='0' && s<='9')) s='a';
line[i] = s;
}
unsigned int *a=(unsigned int *)str;
char m=0;
while(m==0){
unsigned int k=*a++;
m=Tbl[(unsigned short)k];
if(m!=0) break;
m=Tbl[k>>16];
}
return (m&1)!=0;
static char *Tbl=0;
bool HasDigit4(char *str){
if(Tbl==0){
Tbl=new char[65536];
for(int i=0;i<65536;i++){
int a1=i&255,a2=i>>8;
if(a1==0) Tbl[i]=2;
else Tbl[i]=(a2==0 ? 2 : 0)+(((unsigned int)(a1-48)<10 || (unsigned int)(a2-48)<10) ? 1 : 0);
}
}
char m=0;
if(((int)str&1)!=0) m=Tbl[*(unsigned short *)(str++)];
unsigned short *a=(unsigned short *)str;
while(m==0) m|=Tbl[*a++];
return (m&1)!=0;
}
bool HasDigit(char *s){
int v=0;
while(*s) v|=1<<((unsigned char)(*s++-48));
return (v&1023)!=0;
}
bool HasDigit(char *s){
int v=0;
while(*s) v|=((*s++-48)&255)-10;
return v<0;
}
HASDIG:: 1$: TSTB (R0) BEQ 2$ MOVB (R0)+,R1 SUB #60,R1 CMP R1,#12 BNC 1$ 2$: BIC R0,R0 ADC R0 RTS PC
bool has_digit(const char* s) {
while (*s) if (*s >= '0' && *s <= '9') return true; else ++s;
return false;
}
bool has_digit(const char* s) {
while (*s) if (*s < '0' || *s > '9') ++s; else return true;
return false;
}
if (*s > '9' || *s < '0')
if ((unsigned char)*s > '9' || *s < '0')
bool has_digit(const char* s) {
for (;*s;++s) if ((*s & 0x30) == 0x30 && *s < ':') return true;
return false;
}
Проверить наличие цифр в строке