網頁全端開發JavaScript_0919

字串基本介紹


let first_name = "wilson";
let last_name = "Ren";
console.log(first_name + " " + last_name); 為了讓中間有空格可以加空白引號

將數字與字串做+運算,數字會轉成字串,等於2個字串連接

例如 : 將 "10"+20 數字會被轉成字串,結果會等於 1020

            
              Let n1 = 20;
              Let n2 = 30;
              Let text = “hello”;
              Let n3 = 40;
              Let n4 = 50; 
              console.log ( n1 + n2 );  數字相加,結果是 50
              console.log ( n1 + n2 + text );  會先做數字相加,50再轉成字串,結果是 50hello
              console.log ( n1 + n2 + text + n3 + n4 );  只要與字串相加,數字都會轉成字串,結果是 50hello4050
            
          

/n 可以換行

Number methods 數字方法 (內建函式)


.toString()將數字轉成字串

let a1 = "100";
let a2 = 100;
let a3 = a1 + a2.toString();
alert ( a3 );
結果是 100100


Number( ) 將字串轉成數字

parseInt()將字串轉成整數

let a1 = "100";
let a2 = 100;
let a3 = parseInt(a1) + a2;
alert ( a3 );
結果是 200

parseFloat()將字串轉成浮點數(有小數點的數字)

            let num1 = prompt("請輸入數字1");
            let num2 = prompt("請輸入數字2");
            console.log(num1+num2);   字串相加
            
            num1 = parseInt(num1);
            num2 = parseInt(num2);
            console.log(num1+num2);  整數相加
            
            let num3 = prompt("請輸入數字3");
            let num4 = prompt("請輸入數字4");
            
            num3 = parseFloat(num3);
            num4 = parseFloat(num4);
            console.log(num3+num4);   包含小數點的數字相加
          

.toFixed(n)將數字 四捨五入取到小數點第n位數, 原本類型是數字會轉成字串

例 : 將圓周率取到小數第2位
const pi = 3.1415926535;
console.log( pi.toFixed (2) );
結果是 3.14


typeof查詢類型,後面放要查詢的內容

let age = 27;
console.log ( typeof age.toString());
結果是 string 字串


let age = 27;
console.log ( typeof age );
結果是 number 數字


注意

二進制不能精確表示所有小數,可能導致意外結果
例如: 0.1 + 0.2 === 0.3 結果會是false

String Attributes and Methods 字串方法

關於字串的屬性與應用

.length 長度計算 (回傳字串長度)

let str = “simple”;
console.log ( str.length );
結果為6

*空白格也列入計算


[n] 取出第 n 個字

*若字串中 沒有該索引值的字,會是undefind

let str = “simple”;
console.log ( str [ 0 ] );
結果為s



.indexOf(“ ”) 查詢字串索引值,輸入字串回傳該字的位置(索引值)

*若找不到該字會回傳-1

let str = “simple”;
console.log ( str .indexOf ( “s” ) );
結果為0
console.log ( str .indexOf ( “a” ) );
結果為-1


.trim()去除掉段落的左右空白

            trimString("    hi, mom!  ") 
            trimString(" aa@gmail.com  ")
          
            function trimString(str){
              return str.trim();
            }
          
            結果:
            "hi, mom!"
            "aa@gmail.com"
          

.toLowerCase()暫時將字母都轉成小寫
.toUpperCase()暫時將字母都轉成大寫

let str = “Simple”;
console.log ( str. toLowerCase ( ) );
console.log ( str. toUpperCase ( ) );
結果為
simple
SIMPLE


let str = “Simple”;
str. toUpperCase ( );
console.log ( str );
因為不會改變字串本身,結果會是 Simple

*要改變字串必須用賦值的方式

let str = “Simple”;
str = str. toUpperCase ( );
console.log ( str );
結果會是 SIMPLE


.slice (起始值,終點值)截取字串 (依照索引值)

let str = “simple”;
console.log ( str .slice( 0 , 4 ));
結果為simp

起始值0 (包含自己),從0開始取
終點值4 (不包含自己),只取到4的前一個字,也就是3

若只有1個數會視為起始值,取到最後1個字
str .slice( 2 ) 結果為 mple

可以負數取值
str .slice( -3 )
結果為 ple

.substring (起始值,終點值)截取字串 (依照索引值)

substring、slice 基本上用法一樣,差別在幾個特殊情況
當起始位置 > 終點位置時
slice 會回傳空字串
substring 會將起始位置與終點位置交換

當終點位置為負數時
slice 正常輸出字串,可以使用負數索引
substring 會將負數換成0

.substr (起始值,取出幾個字串)截取字串 (依照索引值)

第2個參數並非終點位置,是取出幾個字串
第2個參數為負數時視為0,也就是取出0個字串 (空字串)
第1個參數可以使用負數索引


.split ()字串轉陣列 (依照條件)

切割字串,依照括號內放的內容來切割字串,分割後的字串會裝進陣列

              function splitString(str){
                return str.split(",");
              }
            
              splitString("black,red,white")
              splitString("coffee,tea")
            
              // ["black","red","white"]
              // ["coffee","tea"]  
            

let sentence = “Today is a good day”;
let result = sentence.split (“ is ”);
console.log (result);

結果為
["To day ", " a good day"]

*用來分割的字會消失


*用空格來分割字串

let para = “Today is a good day”;
console.log (pra.split(" "));

結果為
["Today", "is", "a", "good", "day"]

let name = "Selina";
console.log(name.split("l"))

結果為
["Se", "ina"]


console.log(name.split())

結果為
["Selina"]


console.log(name.split(""))

結果為
["S", "e", "l", "i", "n", "a"]

*放空字串可切割每個單字

.join ( )陣列 轉字串(依照條件)

將陣列元素轉換成字串
括號內->用什麼字元來串接陣列裡的元素

str = [ 123, 456, 789 ];
console.log ( str .join ( '' ) ); // "123456789"
console.log ( str .join ( '-') ); // "123-456-789"


.startsWith(“s”) 查開頭是否為s,有大小寫之分

.endsWith(“s”) 查結尾是否為s,有大小寫之分

*會回傳true 或 false

let sentence = “Today is a good day”;
console.log ( sentence .startsWith (“Today”) );
結果是 true


.includes(str) 查詢字串是否包含str

*會回傳true 或 false

let sentence = “Today is a good day”;
console.log ( sentence .includes (“good”) );
結果是 true


.charAt( ) 用位置(索引值)查詢內容、字元

let sentence = “Today is a good day”;
console.log ( sentence .charAt ( 2 ) );
結果為 d

.charCodeAt( ) 用位置(索引值)來取得字元的 Unicode 編碼