Fastest Way to Check If a Character Is a Digit

What is the best way to tell if a character is a letter or number in Java without using regexes?

Character.isDigit(string.charAt(index)) (JavaDoc) will return true if it's a digit

Character.isLetter(string.charAt(index)) (JavaDoc) will return true if it's a letter

Fastest way to check if a character is a digit?

I'd be very surprised if you would ever be able to detect any difference between WHERE col LIKE '[0-9]' and any other methods you come up with. But I agree with Denis, put that away in a function so that you use the same check consistently throughout all your code (or at least, if you're avoiding UDFs because of large scans etc., put a marker in your code that will make it easy to change on a wide scale later).

That said, you are most certainly going to see more of a performance hit just by using a scalar UDF than what method you use to parse inside the function. You really ought to compare performance of the UDF vs. doing that inline using CASE. e.g.

SELECT Postal = CONVERT(INT, CASE WHEN SUBSTRING(postal,2,1) LIKE '[0-9]' 
THEN SUBSTRING(postal, 2,1) END)
FROM ...

This will yield NULL if the character is not numeric.

If you are only dealing with checking local variables, it really is not going to matter what parsing method you use, and you are better off focusing your optimization efforts elsewhere.

EDIT adding suggestion to demonstrated JOIN clause. This will potentially lead to less constant scans but is a lot more readable (far fewer substring calls etc):

;WITH v AS 
(
SELECT /* other columns, */ patientPostal,
ss = SUBSTRING(v.patientPostal,2,1),
FROM [whatever table is aliased v in current query]
)
SELECT /* column list */
FROM [whatever table is aliased z in current query]
INNER JOIN v ON z.postal = CONVERT(INT, CASE
WHEN v.ss = '0' THEN ss
WHEN v.ss LIKE '[1-9]' THEN LEFT(v.patientPostal, 3)
END);

Determine if char is a num or letter

You'll want to use the isalpha() and isdigit() standard functions in <ctype.h>.

char c = 'a'; // or whatever

if (isalpha(c)) {
puts("it's a letter");
} else if (isdigit(c)) {
puts("it's a digit");
} else {
puts("something else?");
}

Check if a char is a digit? (in C)

The trick is that the isdigit function does not take an argument of type char. Quoting the standard (N1570 7.4p1:

The header <ctype.h> declares several functions useful for
classifying and mapping characters. In all cases the argument is an
int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the
argument has any other value, the behavior is undefined.

The type char may be either signed or unsigned. If it's signed (as it very commonly is), then it can hold negative values -- and passing a negative value other than EOF (typically -1) to isdigit, or to any of the other functions declared in <ctype.h>, causes undefined behavior.

The solution is to convert the argument to unsigned char before passing it to isdigit:

char c = -46;
if (isdigit((unsigned char)c) {
puts("It's a digit (?)");
}
else {
puts("It's not a digit");
}

And yes, this is exactly as annoying and counterintuitive as you think it is.

Fastest way to check if string contains only digits in C#

bool IsDigitsOnly(string str)
{
foreach (char c in str)
{
if (c < '0' || c > '9')
return false;
}

return true;
}

Will probably be the fastest way to do it.

How to check if last character of a string is a digit/number by the fastest way in plain JavaScript?

You can use Conditional (ternary) operator with isNaN() and String.prototype.slice():

function endsWithNumber( str ){  return isNaN(str.slice(-1)) ? 'does NOT end with a number' : 'ends with a number';}
console.log(endsWithNumber('Pocahontas'));console.log(endsWithNumber('R2D2'));


Related Topics



Leave a reply



Submit