如何检查JavaScript中的var是否是字符串?

时间:2022-08-26 22:04:58

How can I check if a var is a string in JavaScript?

如何检查JavaScript中的var是否是字符串?

I've tried this and it doesn't work...

我试过了,但没用……

var a_string = "Hello, I'm a string.";

if (a_string typeof 'string') {
    // this is a string
}

6 个解决方案

#1


285  

You were close:

你是亲密:

if (typeof a_string === 'string') {
    // this is a string
}

On a related note: the above check won't work if a string is created with new String('hello') as the type will be Object instead. There are complicated solutions to work around this, but it's better to just avoid creating strings that way, ever.

相关说明:如果使用新字符串('hello')创建一个字符串,那么上面的检查将不起作用,因为类型将改为Object。有一些复杂的解决方案可以解决这个问题,但是最好还是避免用这种方式创建字符串。

#2


65  

The typeof operator isn't an infix (so the LHS of your example doesn't make sense).

类型运算符不是中缀(因此示例的LHS没有意义)。

You need to use it like so...

你需要像这样使用它……

if (typeof a_string == 'string') {
    // This is a string.
}

Remember, typeof is an operator, not a function. Despite this, you will see typeof(var) being used a lot in the wild. This makes as much sense as var a = 4 + (1).

记住,typeof是运算符,不是函数。尽管如此,您将看到typeof(var)在野外被大量使用。这和var a = 4 +(1)一样有意义。

Also, you may as well use == (equality comparison operator) since both operands are Strings (typeof always returns a String), JavaScript is defined to perform the same steps had I used === (strict comparison operator).

此外,您还可以使用==(相等比较运算符),因为这两个操作数都是字符串(类型总是返回一个字符串),JavaScript被定义为执行相同的步骤,我使用==(严格的比较运算符)。

As Box9 mentions, this won't detect a instantiated String object.

正如Box9提到的,这不会检测实例化的字符串对象。

You can detect for that with....

你可以检测与....

var isString = str instanceof String;

jsFiddle.

jsFiddle。

...or...

…或…

var isString = str.constructor == String;

jsFiddle.

jsFiddle。

But this won't work in a multi window environment (think iframes).

但这在多窗口环境中是行不通的(想想iframe)。

You can get around this with...

你可以用……

var isString = Object.prototype.toString.call(str) == '[object String]';

jsFiddle.

jsFiddle。

But again, (as Box9 mentions), you are better off just using the literal String format, e.g. var str = 'I am a string';.

但是,(就像Box9提到的那样),您最好使用文字字符串格式,例如var str = 'I am a String ';

Further Reading.

进一步阅读。

#3


8  

Combining the previous answers provides these solutions:

结合前面的答案提供了以下解决方案:

if (typeof str == 'string' || str instanceof String)

or

Object.prototype.toString.call(str) == '[object String]'

#4


0  

Now days I believe it's preferred to use a function form of typeof() so...

现在我认为它更喜欢使用typeof()的函数形式,所以……

if(filename === undefined || typeof(filename) !== "string" || filename === "") {
   console.log("no filename aborted.");
   return;
}

#5


0  

check for null or undefined in all cases a_string

检查所有情况下是否为空或未定义的a_string

if (a_string && typeof a_string === 'string') {
    // this is a string and it is not null or undefined.
}

#6


-3  

My personal approach, which seems to work for all cases, is testing for the presence of members that will all only be present for strings.

我个人的方法似乎适用于所有情况,就是测试成员的存在,这些成员只会出现在字符串中。

function isString(x) {
    return (typeof x == 'string' || typeof x == 'object' && x.toUpperCase && x.substr && x.charAt && x.trim && x.replace ? true : false);
}

See: http://jsfiddle.net/x75uy0o6/

参见:http://jsfiddle.net/x75uy0o6/

I'd like to know if this method has flaws, but it has served me well for years.

我想知道这种方法是否有缺点,但它多年来一直很管用。

#1


285  

You were close:

你是亲密:

if (typeof a_string === 'string') {
    // this is a string
}

On a related note: the above check won't work if a string is created with new String('hello') as the type will be Object instead. There are complicated solutions to work around this, but it's better to just avoid creating strings that way, ever.

相关说明:如果使用新字符串('hello')创建一个字符串,那么上面的检查将不起作用,因为类型将改为Object。有一些复杂的解决方案可以解决这个问题,但是最好还是避免用这种方式创建字符串。

#2


65  

The typeof operator isn't an infix (so the LHS of your example doesn't make sense).

类型运算符不是中缀(因此示例的LHS没有意义)。

You need to use it like so...

你需要像这样使用它……

if (typeof a_string == 'string') {
    // This is a string.
}

Remember, typeof is an operator, not a function. Despite this, you will see typeof(var) being used a lot in the wild. This makes as much sense as var a = 4 + (1).

记住,typeof是运算符,不是函数。尽管如此,您将看到typeof(var)在野外被大量使用。这和var a = 4 +(1)一样有意义。

Also, you may as well use == (equality comparison operator) since both operands are Strings (typeof always returns a String), JavaScript is defined to perform the same steps had I used === (strict comparison operator).

此外,您还可以使用==(相等比较运算符),因为这两个操作数都是字符串(类型总是返回一个字符串),JavaScript被定义为执行相同的步骤,我使用==(严格的比较运算符)。

As Box9 mentions, this won't detect a instantiated String object.

正如Box9提到的,这不会检测实例化的字符串对象。

You can detect for that with....

你可以检测与....

var isString = str instanceof String;

jsFiddle.

jsFiddle。

...or...

…或…

var isString = str.constructor == String;

jsFiddle.

jsFiddle。

But this won't work in a multi window environment (think iframes).

但这在多窗口环境中是行不通的(想想iframe)。

You can get around this with...

你可以用……

var isString = Object.prototype.toString.call(str) == '[object String]';

jsFiddle.

jsFiddle。

But again, (as Box9 mentions), you are better off just using the literal String format, e.g. var str = 'I am a string';.

但是,(就像Box9提到的那样),您最好使用文字字符串格式,例如var str = 'I am a String ';

Further Reading.

进一步阅读。

#3


8  

Combining the previous answers provides these solutions:

结合前面的答案提供了以下解决方案:

if (typeof str == 'string' || str instanceof String)

or

Object.prototype.toString.call(str) == '[object String]'

#4


0  

Now days I believe it's preferred to use a function form of typeof() so...

现在我认为它更喜欢使用typeof()的函数形式,所以……

if(filename === undefined || typeof(filename) !== "string" || filename === "") {
   console.log("no filename aborted.");
   return;
}

#5


0  

check for null or undefined in all cases a_string

检查所有情况下是否为空或未定义的a_string

if (a_string && typeof a_string === 'string') {
    // this is a string and it is not null or undefined.
}

#6


-3  

My personal approach, which seems to work for all cases, is testing for the presence of members that will all only be present for strings.

我个人的方法似乎适用于所有情况,就是测试成员的存在,这些成员只会出现在字符串中。

function isString(x) {
    return (typeof x == 'string' || typeof x == 'object' && x.toUpperCase && x.substr && x.charAt && x.trim && x.replace ? true : false);
}

See: http://jsfiddle.net/x75uy0o6/

参见:http://jsfiddle.net/x75uy0o6/

I'd like to know if this method has flaws, but it has served me well for years.

我想知道这种方法是否有缺点,但它多年来一直很管用。