Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中检查变量是否为字符串?_Javascript_String_Variable Types - Fatal编程技术网

如何在JavaScript中检查变量是否为字符串?

如何在JavaScript中检查变量是否为字符串?,javascript,string,variable-types,Javascript,String,Variable Types,如何在JavaScript中检查变量是否为字符串 我试过了,但没用 var a_string = "Hello, I'm a string."; if (a_string typeof 'string') { // this is a string } 你很接近: if (typeof a_string === 'string') { // this is a string } 请注意:如果使用newstring('hello')创建字符串,则上述检查将不起作用,因为类型

如何在JavaScript中检查变量是否为字符串

我试过了,但没用

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

if (a_string typeof 'string') {
    // this is a string
}
你很接近:

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

请注意:如果使用
newstring('hello')
创建字符串,则上述检查将不起作用,因为类型将改为
Object
。有很多复杂的解决方案可以解决这个问题,但最好不要以这种方式创建字符串。

操作符不是中缀(因此示例中的LHS没有意义)

你需要像这样使用它

if (typeof a_string == 'string') {
    // This is a string.
}
请记住,
typeof
是一个运算符,而不是一个函数。尽管如此,您将看到
typeof(var)
在野外被大量使用。这与var a=4+(1)一样有意义

另外,您也可以使用
==
(相等比较运算符),因为两个操作数都是
字符串(
typeof
始终返回
字符串
),JavaScript被定义为执行与我使用的
==
(严格比较运算符)相同的步骤

这是一个实例化的
字符串
对象

你可以用……来检测

var isString = str instanceof String;

……或者

var isString = str.constructor == String;

但是这在多
窗口
环境中不起作用(想想
iframe
s)

你可以通过

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

但是,同样,您最好只使用literal
String
格式,例如
var str='I am a String'


.

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

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


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

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


我想知道这个方法是否有缺陷,但多年来它一直为我服务。

现在我认为最好使用typeof()的函数形式,以便


检查所有情况下的null或未定义字符串

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

以下表达式返回true:


以下表达式返回true:


以下表达式返回false(原文如此!):


以下表达式返回true:

最佳和正确的方式(imho):


@第九栏别担心,反正我是代表:P@alex我现在也是:o(10分钟以上!)@RobG Rep capped是指你在24小时内获得最大代表次数。在这之后,向上投票不计入您的声誉。是否不可能通过简单地测试只有字符串具有的成员的存在来检查变量是否是字符串?例如:
if(myVar.toUpperCase)警报(“我是一个字符串”)?看:@containment_15939这不是个好办法。。。因为
{toUpperCase:'}
伙计们,我真的想给你们两个一个公认的答案,但我不能,我能做的就是对两个都+1,而不是给一个公认的答案,在谁更接近我的具体问题,我还没有完全解释清楚。这对我有效
如果(typeof(str)==typeof(String())
这很容易被任何具有这些方法的旧对象所愚弄。这被称为duck typing-例如,如果它像字符串一样走路,像字符串一样说话,那么它也可能是字符串。如果你认为这是测试字符串的最佳方法,那你就有点疯狂了,但是Javascript是Thunderdome,你就是。这的可能副本也是的副本。没有函数形式的
typeof
,你只是用括号控制操作顺序。在某些情况下,有些人可能会觉得它更具可读性。@Jonz你所说的“控制操作顺序”是什么意思?谢谢。我想后来我意识到你可以检查构造器,并且更喜欢它,因为理论上我认为它会更快,但不是更快?这里的示例4显示了括号的用法,它对于编译器解析来说是更可读和更少的。我对“顺序”的猜测可能是速度问题,或者与编译器如何加载参数堆栈有关,我不确定。@a20操作顺序描述了包含多个操作的语句执行操作的顺序。请参阅-括号(操作分组)具有最高的运算符优先级,因此首先计算括号。在本例中,
filename
周围的括号只对一条语句进行分组,因此是无用的和无关的。这个答案的分数是0,这是一件好事,因为它是错误的、误导性的、没有帮助的;如果它有一个负的分数会更好。非常有用的链接感谢。所以括号被选中并首先运行?因此,应该立即运行,而不检查next是否有其他调用aka的方法(不带括号),这将是更晚和更慢的步骤?不我对那里的运行时编译器有什么不了解。
typeof null
typeof undefined
永远不会返回
'string'
,所以
typeof a_string
就足够了。抱歉发帖了
if(filename === undefined || typeof(filename) !== "string" || filename === "") {
   console.log("no filename aborted.");
   return;
}
if (a_string && typeof a_string === 'string') {
    // this is a string and it is not null or undefined.
}
'qwe'.constructor === String
typeof 'qwe' === 'string'
typeof new String('qwe') === 'string'
typeof new String('qwe').valueOf() === 'string'
if (someVariable.constructor === String) {
   ...
}