--- title: 字串 tags: string, trim, length, description: --- 字串 === ### 字串的宣告 宣告變數賦予字串時,可在文字前後加上==單引號''==或==雙引號""==。 ```javascript= let name = 'joe'; let name = "joe"; ``` 文字前後必須統一,==不可'文字"或"文字'== ```javascript= let name = 'joe"; let name = "joe'; ``` 如果因文字符號需求,例如Let's go,則在==文字前後加上雙引號==。 ```javascript= let content = "Let's go"; ``` <br> ### 字串的相加 可將字串賦予到變數上,再經由==變數加上變數==的方式做呈現。 或者用==字串加字串==的方式做呈現。 ```javascript= let name = 'john'; let content = '你好嗎'; let total = name + content; console.log(total); //john你好嗎 console.log('john' + '你好嗎'); //john你好嗎 ``` <br> ### `typeof`瞭解變數的型別 透過`typeof`來==確認變數的型別==。 ```javascript= let name = 'john'; let age = 19; let isActive = false; console.log(typeof name); //string(字串型別) console.log(typeof 19); //number(數字型別) console.log(typeof isActive); //boolean(布林型別) ``` <br> ### number與string相加 - #### 自動轉型 某些情況下JavaScript會==自動幫你轉型== ```javascript= let name = 'john'; let age = 19; let content = name + age console.log(typeof name); //string(字串型別) console.log(typeof 19); //number(數字型別) console.log(typeof content); //string(字串型別) ``` 在範例中看到,變數`content`最後經由`typeof`確認型別,結果為string(字串型別)。 因為在JavaScript嘗試連接==字串型別==加==數字型別==時,會==自動轉型為string==。 <br> ### NaN 非數字 - #### 什麼是NaN? 當明明無法用==加減乘除==處理時,但==仍企圖用加減乘除來處理,所呈現的結果==。 <br> 下面的範例,```變數total```等於```string```乘上```number```,得知變數```total```型別為```number```,但值為==NaN==。 ```javascript= let num1 = '二十'; let num2 = 15; let total = num1 / num2; console.log(typeof total); //number console.log(total); //NaN ``` <br> 在這個範例,使用了```parseInt()```把字串轉型為```number```, 確認變數```num1``` ```num2```都為```number```型別,```num1```有成功轉型,值為==20==。 ```num2```沒有成功轉型,值為==NaN==。 ```javascript= let num1 = parseInt('20'); let num2 = parseInt('二十'); console.log(typeof num1); //number console.log(typeof num2); //number console.log(num1); //20 console.log(num2); //NaN ``` ### 字串處理實用方法 - #### 檢查字串的字元數 變數名稱加上```.length```,得知==字串的字元數==。 ```javascript= let num = '1234567890'; console.log(num.length); //10 ``` - #### 去除字串前後空白字元 變數名稱加上```.trim()```,==去除字串前後多餘的空白==。 ```javascript= let name = ' mark '; console.log(name.length); //6 console.log(name.trim()); //mark ``` - #### 字串轉數字 用``parseInt()``把字串轉成數字 ```javascript= let a = '1'; a = parseInt(a); // 把變數a轉成數字,再重新賦予給變數a console.log(a + 1); // 2 ``` <br> 如果把``變數a``賦予``'z'``,經過``parseInt()``結果會是``NaN`` ```javascript= let a = 'z'; a = parseInt(a); console.log(a); // NaN ``` - #### 數字轉字串 用```.toString()```把字串轉成數字 ```javascript= let a = 1234; a = a.toString(); // 把變數a轉成字串,再重新賦予給變數a console.log(a + 1); //12341 ``` <br> 用```.toString()```把10進制數字轉成16進制數字 - ```.toString()```括號()代入16,代表==轉換成16進制==數字(型別字串) - ```+a.toString(16)```,加上+號,把string型別轉型成number型別 ```javascript= let a = 16; a = +a.toString(16); console.log(typeof a); // number console.log(a); // 10 => 0001 0000 ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up