String資料型別轉換

2021-10-07 14:10:16 字數 1183 閱讀 3266

js一共有6種資料型別

string:字串 typeof-string

number:數值 typeof-number

(nan是乙個特殊的數字,表示不是數字)

boolean:布林值 typeof-boolean

null:空值 typeof-object(特殊)

undefined:未定義 typeof-undefined

object:物件 typeof-object

其中前5屬於基本資料型別,object屬於引用資料型別

var a=

"abc"

+"bcd"

;console.

log(

typeof a)

;//輸出的會是nan

其他型別(number boolean)轉換為string

tostring()方法

var a =

123;

a = a.

tostring()

;console.

log(

typeof a)

;//輸出string

console.

log(a)

;//輸出123,此為字串型

var a =

null

;//var a = undefined;

a = a.

tostring()

;console.

log(a)

;//會報錯

console.

log(

typeof a)

;//會報錯

呼叫string()函式–並將我們被轉換的資料作為引數傳遞給函式 在轉換number和boolean與方法一一致,區別是它可以對null和undefined也進行強轉

var a =

null

;//var a = undefined;

a =string

(a);

console.

log(a)

;//輸出的是字串 null

console.

log(

typeof a)

;//輸出的是型別 string undefined也同樣可以

資料型別 string

單引號建立字串 str0 php xust echo str0 echo 單引號中的單引號需要轉義 雙引號建立字串與單引號不同 可以解釋變數 str1 php echo str1 echo str3 php echo str3 echo 用heredoc 語法來建立字串 適用於大量字串的情況 優化的...

String 和其他資料型別的轉換

將其它基本資料型別轉換為string 型別 一 由基本資料型態轉換成string string 類別中已經提供了將基本資料型態轉換成 string 的static 方法,也就是string.valueof 這個引數多載的方法 有以下幾種 1 string.valueof boolean b 將boo...

複雜的資料型別 string資料型別

include include 為了使用getline函式和在我們的程式中使用string型別,使用string型別時我們需要包含string字元標頭檔案 using namespace std define item 10 使用巨集定義變數 void calcsum void string int...