Check If String Is Just White Space

How to detect string which contains only spaces?

To achieve this you can use a Regular Expression to remove all the whitespace in the string. If the length of the resulting string is 0, then you can be sure the original only contained whitespace. Try this:

var str = "    ";if (!str.replace(/\s/g, '').length) {  console.log('string only contains whitespace (ie. spaces, tabs or line breaks)');}

Check if string contains only whitespace

Use the str.isspace() method:

Return True if there are only whitespace characters in the string and there is at least one character, False otherwise.

A character is whitespace if in the Unicode character database (see unicodedata), either its general category is Zs (“Separator, space”), or its bidirectional class is one of WS, B, or S.

Combine that with a special case for handling the empty string.

Alternatively, you could use str.strip() and check if the result is empty.

How can I check if string contains characters & whitespace, not just whitespace?

Instead of checking the entire string to see if there's only whitespace, just check to see if there's at least one character of non whitespace:

if (/\S/.test(myString)) {
// string is not empty and not just whitespace
}

Check whether a string is empty (or spaces)

You do not have a string containing only whitespace characters. You have a bytestring containing the UTF-8 encoding of a Unicode string containing only whitespace characters.

Decoding the bytes in UTF-8 produces a Unicode string that reports True for isspace:

>>> a.decode('utf-8').isspace()
True

but don't just slap decode('utf-8') into your code ad-hoc and hope it works.

Keep track of whether you're using Unicode or bytestrings at all times. Generally, work in Unicode, convert bytestring input to Unicode immediately, and only convert Unicode to bytestrings as it leaves your code.

Check if a string has white space

You can simply use the indexOf method on the input string:

function hasWhiteSpace(s) {
return s.indexOf(' ') >= 0;
}

Or you can use the test method, on a simple RegEx:

function hasWhiteSpace(s) {
return /\s/g.test(s);
}

This will also check for other white space characters like Tab.

Check if a string has only white spaces in it

You could use trimws, which removes leading/trailing white-space from a character string:

trimws(str) == ""
#[1] TRUE

How to check if a string contains only white space or is empty in TypeScript?

The error Type 'boolean' is not assignable to type 'string' is due to the function signature indicating that the return type is a string when it is actually returning a boolean.

The definition should be:

export const isEmpty = function(text: string): boolean {
return text === null || text.match(/^ *$/) !== null;
};

Whitespace Catch-All

Note that whitespace may be any of the following:

  • space character
  • tab character
  • carriage return character
  • new line character
  • vertical tab character
  • form feed character

To handle these cases, the regular expression should be:

/^\s*$/

The function could be written:

export function isEmpty(text: string): boolean {
return text == null || text.match(/^\s*$/) !== null;
}

How do I check that a Java String is not all whitespaces?

Shortest solution I can think of:

if (string.trim().length() > 0) ...

This only checks for (non) white space. If you want to check for particular character classes, you need to use the mighty match() with a regexp such as:

if (string.matches(".*\\w.*")) ...

...which checks for at least one (ASCII) alphanumeric character.

Check if a string has only whitespace characters in C

So far, the only solution that I've thought of is to loop over the array, and check each individual char with isspace().

That sounds about right!

Is there a more efficient way?

Not really. You need to check each character if you want to be sure only space is present. There could be some trick involving bitmasks to detect non-space characters in a faster way (like strlen() does to find a NUL terminator), but I would definitely not advise it.

You could make use of strspn() or strcspn() checking the returned value, but that would surely be slower since those functions are meant to work on arbitrary accept/reject strings and need to build lookup tables first, while isspace() is optimized for its purpose using a pre-built lookup table, and will most probably also get inlined by the compiler using proper optimization flags. Other than this, vectorization of the code seems like the only way to speed things up further. Compile with -O3 -march=native -ftree-vectorize (see also this post) and run some benchmarks.



Related Topics



Leave a reply



Submit