indexOf()
indexOf() 매서드는 호출한 String 객체에서 주어진 값과 일치하는 첫 번째 인덱스를 반환.
일치하는 값이 없으면 -1반환
const result = 'Hello world'.indexOf('world')
console.log(result)
// 결과 : 6
매개변수
searchValue
fromeIndex
만약 Boolean처럼 참,거짓의 값을 출력하고 싶을 때
const str = 'Hello world'
console.log(str.indexOf('HEROPY') !== -1)
// 부정일치 선택자니까 false
length()
length
속성은 UTF-16 코드 유닛을 기준으로 (공백포함)문자열의 길이를 나타냅니다.
const str = 'Life, the universe and everything. Answer:';
console.log(`${str} ${str.length}`);
// Expected output: "Life, the universe and everything. Answer: 42"
일반적인 사용법
var x = 'Mozilla';
var empty = '';
console.log('Mozilla는 코드 유닛 ' + x.length + '개의 길이입니다.');
/* "Mozilla는 코드 유닛 7개의 길이입니다." */
console.log('빈 문자열은 ' + empty.length + '의 길이를 가집니다.');
/* "빈 문자열은 0의 길이를 가집니다." */
slice()
slice()
메소드는 문자열의 일부를 추출하면서 새로운 문자열을 반환합니다. (zero-based)
str.slice(beginIndex[, endIndex])
const str = 'Hello world'
console.log(str.slice(0,3))
//Hel
예시
slice()를 사용하여 새 문자열 생성하기.
var str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice(1, 8),
str3 = str1.slice(4, -2),
str4 = str1.slice(12),
str5 = str1.slice(30);
console.log(str2); // OUTPUT: he morn
console.log(str3); // OUTPUT: morning is upon u
console.log(str4); // OUTPUT: is upon us.
console.log(str5); // OUTPUT: ""
replace()
replace()
메서드는 어떤 패턴에 일치하는 일부 또는 모든 부분이 교체된 새로운 문자열을 반환
const str = 'monamo@naver.com.'
console.log(str.match(/.+(?=@)/)[0])
//위의 정규 표현식 해석
//@를 기준으로 ()에 있는 내용이 @기호에 앞에있는 문자를 일치 시킬것인데 .(한글자)들 중에 최대한 많이(+) 일치 시킬 것
var newStr = str.replace(regexp|substr, newSubstr|function)
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey'));
// Expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// Expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
예제
var str = 'Twas the night before Xmas...';
var newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr); // Twas the night before Christmas...
trim()
trim()
메서드는 문자열 양 끝의 공백을 제거하고 원본 문자열을 수정하지 않고 새로운 문자열을 반환합니다.
const str = ' Hello world '
console.log(str)
// Hello world
console.log(str.trim())
//Hello world
'JavaScript' 카테고리의 다른 글
[JS] 배열API (2) | 2024.02.28 |
---|---|
[JS] JS 숫자데이터(Number) (0) | 2023.03.15 |
[JS] JS 생성자와 프로토타입 (0) | 2023.03.14 |
[JS] JS 스코프(Scope) 유효범위 가볍게 이해하기 (0) | 2023.03.14 |
[JS] 콜백(Callback) 함수란? (0) | 2023.02.23 |
indexOf()
indexOf() 매서드는 호출한 String 객체에서 주어진 값과 일치하는 첫 번째 인덱스를 반환.
일치하는 값이 없으면 -1반환
const result = 'Hello world'.indexOf('world')
console.log(result)
// 결과 : 6
매개변수
searchValue
fromeIndex
만약 Boolean처럼 참,거짓의 값을 출력하고 싶을 때
const str = 'Hello world'
console.log(str.indexOf('HEROPY') !== -1)
// 부정일치 선택자니까 false
length()
length
속성은 UTF-16 코드 유닛을 기준으로 (공백포함)문자열의 길이를 나타냅니다.
const str = 'Life, the universe and everything. Answer:';
console.log(`${str} ${str.length}`);
// Expected output: "Life, the universe and everything. Answer: 42"
일반적인 사용법
var x = 'Mozilla';
var empty = '';
console.log('Mozilla는 코드 유닛 ' + x.length + '개의 길이입니다.');
/* "Mozilla는 코드 유닛 7개의 길이입니다." */
console.log('빈 문자열은 ' + empty.length + '의 길이를 가집니다.');
/* "빈 문자열은 0의 길이를 가집니다." */
slice()
slice()
메소드는 문자열의 일부를 추출하면서 새로운 문자열을 반환합니다. (zero-based)
str.slice(beginIndex[, endIndex])
const str = 'Hello world'
console.log(str.slice(0,3))
//Hel
예시
slice()를 사용하여 새 문자열 생성하기.
var str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice(1, 8),
str3 = str1.slice(4, -2),
str4 = str1.slice(12),
str5 = str1.slice(30);
console.log(str2); // OUTPUT: he morn
console.log(str3); // OUTPUT: morning is upon u
console.log(str4); // OUTPUT: is upon us.
console.log(str5); // OUTPUT: ""
replace()
replace()
메서드는 어떤 패턴에 일치하는 일부 또는 모든 부분이 교체된 새로운 문자열을 반환
const str = 'monamo@naver.com.'
console.log(str.match(/.+(?=@)/)[0])
//위의 정규 표현식 해석
//@를 기준으로 ()에 있는 내용이 @기호에 앞에있는 문자를 일치 시킬것인데 .(한글자)들 중에 최대한 많이(+) 일치 시킬 것
var newStr = str.replace(regexp|substr, newSubstr|function)
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey'));
// Expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// Expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
예제
var str = 'Twas the night before Xmas...';
var newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr); // Twas the night before Christmas...
trim()
trim()
메서드는 문자열 양 끝의 공백을 제거하고 원본 문자열을 수정하지 않고 새로운 문자열을 반환합니다.
const str = ' Hello world '
console.log(str)
// Hello world
console.log(str.trim())
//Hello world
'JavaScript' 카테고리의 다른 글
[JS] 배열API (2) | 2024.02.28 |
---|---|
[JS] JS 숫자데이터(Number) (0) | 2023.03.15 |
[JS] JS 생성자와 프로토타입 (0) | 2023.03.14 |
[JS] JS 스코프(Scope) 유효범위 가볍게 이해하기 (0) | 2023.03.14 |
[JS] 콜백(Callback) 함수란? (0) | 2023.02.23 |