检测仅包含空格的字符串

时间:2022-07-03 21:37:47

A string length which contains one space is always equal to 1:

包含一个空格的字符串长度始终等于1:

alert('My str length: ' + str.length);

The space is a character, so:

空间是一个角色,所以:

str = "   ";
alert('My str length:' + str.length); // My str length: 3

How can I make a distinction between an empty string and a string which contains only spaces? How can I detect a string which contain only spaces?

如何区分空字符串和仅包含空格的字符串?如何检测仅包含空格的字符串?

5 个解决方案

#1


63  

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:

要实现此目的,您可以使用正则表达式删除字符串中的所有空格。如果结果字符串的长度为0,那么您可以确定原始字符串只包含空格。尝试这个:

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

Example fiddle

#2


17  

The fastest solution would be using the regex prototype function test() and looking for any character that is not a space or line break \S :

最快的解决方案是使用正则表达式原型函数test()并查找任何不是空格或换行符的字符\ S:

if (/\S/.test(str))
{
    // found something other than a space or line break
}

In case you have a super long string, it can make a significant difference.

如果你有一个超长字符串,它可以产生显着的差异。

#3


3  

You can Trim your String value by creating a trim function for your Strings.

您可以通过为字符串创建trim函数来修剪String值。

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

now it will be available for your every String and you can use it as

现在它将可用于您的每个String,您可以将其用作

str.trim().length// Result will be 0

You can also use this method to remove the white spaces at the start and end of the String i.e

您也可以使用此方法删除字符串开头和结尾的空格,即

"  hello  ".trim(); // Result will be "hello"

#4


3  

Removing the whitespace from a String can be done using String.trim().

可以使用String.trim()从String中删除空格。

function isNullOrEmpty(str){
    return !str.trim();
}

This method can be used to check if a String contains only spaces and it can be invoked every time like this:

此方法可用于检查String是否仅包含空格,并且每次都可以调用它,如下所示:

var str = document.getElementById("txt").value;
if(isNullOrEmpty(str)){
     console.log("String is empty or contains only spaces.");
}

Demo:

function isNullOrEmpty(str){
    return !str.trim();
}
const resultElem = document.getElementById("result");
const inputElem = document.querySelector('input');
function testEmpty(){
  if(isNullOrEmpty(inputElem.value)){
  result.textContent = "Input is an empty String or a String containing only spaces.";
  } else {
    result.textContent = "Input is not an empty String or a String containing only spaces.";
  }
}
<input type="text">
<br/>
<button onClick="testEmpty()">Check</button>
<p/>
<span id="result"></span>

#5


0  

Trim your String value by creating a trim function

通过创建trim函数修剪String值

var text = "  ";
if($.trim(text.length == 0){
  console.log("Text is empty");
}
else
{
  console.log("Text is not empty");
}

#1


63  

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:

要实现此目的,您可以使用正则表达式删除字符串中的所有空格。如果结果字符串的长度为0,那么您可以确定原始字符串只包含空格。尝试这个:

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

Example fiddle

#2


17  

The fastest solution would be using the regex prototype function test() and looking for any character that is not a space or line break \S :

最快的解决方案是使用正则表达式原型函数test()并查找任何不是空格或换行符的字符\ S:

if (/\S/.test(str))
{
    // found something other than a space or line break
}

In case you have a super long string, it can make a significant difference.

如果你有一个超长字符串,它可以产生显着的差异。

#3


3  

You can Trim your String value by creating a trim function for your Strings.

您可以通过为字符串创建trim函数来修剪String值。

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

now it will be available for your every String and you can use it as

现在它将可用于您的每个String,您可以将其用作

str.trim().length// Result will be 0

You can also use this method to remove the white spaces at the start and end of the String i.e

您也可以使用此方法删除字符串开头和结尾的空格,即

"  hello  ".trim(); // Result will be "hello"

#4


3  

Removing the whitespace from a String can be done using String.trim().

可以使用String.trim()从String中删除空格。

function isNullOrEmpty(str){
    return !str.trim();
}

This method can be used to check if a String contains only spaces and it can be invoked every time like this:

此方法可用于检查String是否仅包含空格,并且每次都可以调用它,如下所示:

var str = document.getElementById("txt").value;
if(isNullOrEmpty(str)){
     console.log("String is empty or contains only spaces.");
}

Demo:

function isNullOrEmpty(str){
    return !str.trim();
}
const resultElem = document.getElementById("result");
const inputElem = document.querySelector('input');
function testEmpty(){
  if(isNullOrEmpty(inputElem.value)){
  result.textContent = "Input is an empty String or a String containing only spaces.";
  } else {
    result.textContent = "Input is not an empty String or a String containing only spaces.";
  }
}
<input type="text">
<br/>
<button onClick="testEmpty()">Check</button>
<p/>
<span id="result"></span>

#5


0  

Trim your String value by creating a trim function

通过创建trim函数修剪String值

var text = "  ";
if($.trim(text.length == 0){
  console.log("Text is empty");
}
else
{
  console.log("Text is not empty");
}