2011-04-21 131 views
66

我想嘗試在JavaScript中進行等效於C#String.IsNullOrEmpty(string)的字符串調用。我在網上看起來假設有一個簡單的電話打來,但我找不到一個。C#String.IsNullOrEmpty Javascript等效

現在我使用的是if(string === "" || string === null)語句來覆蓋它,但我寧願使用預定義的方法(我一直得到某些情況下,由於某種原因溜走)

什麼是最接近的JavaScript(或jQuery的,如果那麼有一個)的電話會是平等的?

+0

可能的重複[什麼是最好的方式來測試一個空字符串jquery-out-the-bo X?(http://stackoverflow.com/questions/1812245/what-is-the-best-way-to-test-for-an-empty-string-with-jquery-out-of-the-box) – 2011-04-21 16:28:41

+3

@ DanielA.White參數是一樣的,但對於像我這樣的C#開發人員來說,這個問題(帶有它的標題)更可能被發現 – 2013-09-07 15:58:00

回答

155

你過度了。空字符串和空字符串都是JavaScript中的錯誤值。

if(!theString) { 
alert("the string is null or empty"); 
} 

Falsey:

  • 不確定
  • 空字符串 ''
  • 數0
  • 數爲NaN
+10

令人驚訝的是,得到了這麼多贊成票。這個答案是不正確的。你說自己一個虛假的值將是0.如果用戶在字段中鍵入0,這不是一個空字符串。空字符串實際上是一個「空」字符串。 – KingOfHypocrites 2015-03-19 19:28:08

+2

如果用戶輸入0,值將是「0」字符串不是數字0.所以它仍然是真的我猜。請張貼您的最佳解決方案,KingOfHypocrites? – 2015-03-27 06:06:33

+2

KingOfHypocrites是正確的,因爲字符串「0」會給出不正確的結果: 「0」== false返回true !「0」== false返回true – 2016-05-23 12:02:27

5

如果你知道string不是數字,這將工作:

if (!string) { 
    . 
    . 
    . 
+0

字符串「0」是falsey,所以你不必檢查if該字符串是數字。 – JoJo 2011-04-21 17:49:36

+0

我的意思是「0」是truthy。 – JoJo 2011-04-21 22:32:14

+1

是的,字符串「0」是truthy,但如果變量'string'實際上不是一個字符串,而是* Number *零,那麼'string'將是falsey。如果我們知道'string'是一個實際的字符串或null,它就不會成爲問題。 – awm 2011-04-22 01:10:40

11

如果出於某種原因,你想測試僅nullempty,你可以做:

function isNullOrEmpty(s) 
{ 
    return (s == null || s === ""); 
} 

注意:這也會在評論中提到未定義的@Raynos。

+0

可以優化爲'return(s == null || s ===「」)' – Raynos 2011-04-21 17:09:42

+0

@Raynos - 爲什麼==而不是===相對於null?如果傳入的對象是未定義的呢? undefined <> null,對嗎? – 2011-04-21 17:12:56

+0

's == null'因爲'null == undefined'而同時捕獲'null'和'undefined' – Raynos 2011-04-21 17:23:54

3

你可以做

if(!string) 
{ 
    //... 
} 

這將檢查string未定義,空,空字符串。

1

要清楚,if(!theString){//...}其中theString是一個未聲明的變量將拋出一個未定義的錯誤,不會發現它是真實的。另一方面,如果您有:if(!window.theString){//...}var theString; if(!theString){//...}它將按預期工作。在變量可能不被聲明的情況下(相對於屬性或簡單地未設置),您需要使用:if(typeof theString === 'undefined'){//...}

我的首選是創建一個包裝它的原型函數。

1

由於被標記爲正確的答案包含一個小錯誤,因此我最好嘗試提出解決方案。我有兩個選擇,一個接受一個字符串,另一個接受一個字符串或一個數字,因爲我認爲很多人在JavaScript中混合字符串和數字。

步驟: - 如果對象爲null,則爲空或空字符串。 - 如果類型不是字符串(或數字),則其字符串值爲空或空。注意:我們也可以在此處引發異常,具體取決於偏好。 - 如果修剪後的字符串值的長度小於1,則爲空或空。

var stringIsNullOrEmpty = function(theString) 
{ 
    return theString == null || typeof theString != "string" || theString.trim().length < 1; 
} 

var stringableIsNullOrEmpty = function(theString) 
{ 
    if(theString == null) return true; 
    var type = typeof theString; 
    if(type != "string" && type != "number") return true; 
    return theString.toString().trim().length < 1; 
} 
1

您可以通過邏輯說

讓說你有一個變量名strVal,檢查是否爲空或空

if (typeof (strVal) == 'string' && strVal.length > 0) 
{ 
// is has a value and it is not null :) 
} 
else 
{ 
//it is null or empty :(
} 
1

您可以創建可以是一個實用方法在很多地方重複使用,如:

function isNullOrEmpty(str){ 
    var returnValue = false; 
    if ( !str 
     || str == null 
     || str === 'null' 
     || str === '' 
     || str === '{}' 
     || str === 'undefined' 
     || str.length === 0) { 
     returnValue = true; 
    } 
    return returnValue; 
    }