달나라 노트

Google Apps Script : includes (문자 포함 여부 판단) 본문

Google Apps Script

Google Apps Script : includes (문자 포함 여부 판단)

CosmosProject 2022. 11. 30. 20:36
728x90
반응형

 

 

 

String 객체의 includes method는 문자열 속에 특정 문자가 포함되어있는지를 판단합니다.

 

 

Syntax

String.includes(text, index)

 

includes method는 어떠한 String에 적용할 수 있습니다.

 

- text

text로 전달된 문자가 String 속에 있는지를 판단하게 됩니다.

 

- index

String에서 text포함되어있는지 판단할 때 시작 위치(index)를 의미합니다.

(index parameter는 생략할 수 있으며 생략한 경우 0으로 간주됩니다. index의 default는 0입니다.)

 

 

text가 String에 포함되어있으면 true를 return하고,

text가 String에 포함되어있지 않으면 false를 return합니다.

 

 

 

 

function myFunction(){
  var test_string = 'Apple_Banana_Peach_Melon';

  Logger.log(test_string.includes('_Bana'));
  Logger.log(test_string.includes('App', 0));
  Logger.log(test_string.includes('App', 1));
  Logger.log(test_string.includes('ppl', 3));
}


-- Result
true
true
false
false

 

- test_string.includes('_Bana')

_Bana라는 글자를 test_string에서 찾습니다.

index parameter가 없으므로 index = 0으로 판단됩니다.

따라서 문자열 처음부터 끝까지 String 전체를 대상으로 _Bana라는 text가 포함되어있는지를 파악합니다.

 

Apple_Banana_Peach_Melon

문자열에 _Bana라는 글자가 포함되어있으므로 true를 return합니다.

 

 

- test_string.includes('App', 0)

App이라는 글자가 test_string에 포함되어있는지 찾습니다.

index parameter가 0이므로 문자 처음부터 끝까지를 대상으로 App이라는 글자를 찾습니다.

 

Apple_Banana_Peach_Melon

문자열에 App이라는 글자가 포함되어있으므로 true를 return합니다.

 

 

- test_string.includes('App', 1)

App이라는 글자가 test_string에 포함되어있는지 찾습니다.
index parameter가 1이므로 index = 1 위치부터 끝까지를 대상으로 App이라는 글자를 찾습니다.

 

index = 1부터 끝까지의 문자열을 나타내보면 아래와 같습니다.

pple_Banana_Peach_Melon

 

문자열에 App이라는 글자가 포함되어있지 않으므로 false를 return합니다.

 

 

- test_string.includes('ppl', 3)

ppl이라는 글자가 test_string에 포함되어있는지 찾습니다.
index parameter가 3이므로 index = 3 위치부터 끝까지를 대상으로 ppl이라는 글자를 찾습니다.

 

index = 3부터 끝까지의 문자열을 나타내보면 아래와 같습니다.

le_Banana_Peach_Melon

 

문자열에 ppl이라는 글자가 포함되어있지 않으므로 false를 return합니다.

 

 

 

 

 

 

728x90
반응형
Comments