문자 하나는 character이며, char이라는 축약어로 부르기도 합니다. char가 포함된 메소드도 있습니다.
MDN 문자열
문자열 블로그
as primitives, from string literals / as objects, using the String() constructor
using '' / "" / `${any}`
string + 다른타입 = string
word1 + " " + word2
let iwantTo = 'behappy';
iwantTo += 'behappy'; // evaluates to 'iwantTobehappy'
let iwantTo = 'behappy';
iwantTo.length; //return 7
iwantTo[0]; ->first letter of..
iwantTo[iwantTo.length-1];->last character of..
iwantTo[0] = 'c'; -> doesn't work( read-only)
<to find, fillter string ..etc>
iwantTo.indexOf('happy'); // 2 ->starts at position 2
<find all instances of strings that don't(or do) contain the substring 'behappy'>
if(iwantTo.indexOf('behappy') === -1) {
// do stuff with the string if the 'behappy'
// substring is NOT contained within it
}
if(iwantTo.indexOf(behappy') !== -1) {
// do stuff with the string if the 'behappy'
// substring IS contained within it
}
'undefined'.indexOf() // return 0, as undefined is found at position 0 in the string undefined.
'undefine'.indexOf() // return -1, as undefined is not found in the string undefine.
For fromIndex values lower than 0, or greater than str.length, the search starts at index 0 and str.length
'beHappy'.indexOf('H',-2) // 2
'beHappy'.indexOf('H', 10) // -1
With no fromIndex value, or any fromIndex value <= string's length : return value is the same as the fromIndex value
'hello world'.indexOf('') // returns 0
'hello world'.indexOf('', 0) // returns 0
'hello world'.indexOf('', 3) // returns 3
any fromIndex value >= the string's length : return the string's length:
'hello world'.indexOf('', 11) // returns 11
'hello world'.indexOf('', 13) // returns 11
'canal'.lastIndexOf(''); // returns 5
'canal'.lastIndexOf('', 2); // returns 2
'cafcaf'.lastIndexOf('caf',0) // 0
'cafcaf'.lastIndexOf('caf',-10) // 0
'cafcaf'.lastIndexOf('caf',10) // 3
const str = 'To be, or not to be, that is the question.'
console.log(str.includes('To be')) // true
console.log(str.includes('nonexistent')) // false
console.log(str.includes('To be', 1)) // false
console.log(str.includes('')) // true
str.slice(beginIndex[, endIndex])
- extracts a section of a string and returns it as a new string, without modifying the original string.
-0, 양수일때 왼-오 방향/ 음수일때 오-왼 방향
let str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice(1, 8) // OUTPUT: he morn
str3 = str1.slice(4, -2)// OUTPUT: morning is upon u
str4 = str1.slice(12)// OUTPUT: is upon us.
str5 = str1.slice(30) // OUTPUT: ""
let str = 'The morning is upon us.'
str.slice(-3) // returns 'us.'
str.slice(0, -1) // returns 'The morning is upon us'
str.slice(-11, 16) // => "is u"
value === NaN // as if it were 0.(0번째 인덱스부터)
The substring() : swaps its two arguments if indexStart > indexEnd
The slice() : returns an empty string if this is the case.
let text = 'Mozilla'
console.log(text.substring(5, 2)) // => "zil"
console.log(text.slice(5, 2)) // => ""
let iwantTo = 'beHappy';
iwantTo.toLowerCase(); // "behappy"
iwantTo.toUpperCase(); //"BEHAPPY"
const iwantTo = iwantTo.replace('Happy','Smart')
(실제 변수값 바꾸려면 할당시켜야)->다른 method들도 마찬가지
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' ');
console.log(words[3]); // "fox"
const chars = str.split('');
console.log(chars[8]); // "k" ->empty 스트링일 경우 단어 하나하나 각각 array
const strCopy = str.split();
console.log(strCopy); // ["The quick brown fox jumps over the lazy dog."] ->아예 빈칸일 경우 스트링 전체가 하나의 array
arr.join([separator])
-creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.
var a = ['Wind', 'Water', 'Fire'];
a.join(); // 'Wind,Water,Fire'
a.join(', '); // 'Wind, Water, Fire'
a.join(' + '); // 'Wind + Water + Fire'
a.join(''); // 'WindWaterFire
const str = 'asdfghjkl'
const strReverse = str.split('').reverse().join('')
// 'lkjhgfdsa'
// split() returns an array on which reverse() and join() can be applied
String (): return 문자열 프리미티브
new String (): 객체 문자열
var x= String('word');
console.log(typeof x); //"string"
var y= new String('word');
console.log(typeof y); //"object"
console.log(new String('')=== new String('')) //false!!!
str1.concat(str2, str3...);
-join two or more strings.
var str1 = "Hello ";
var str2 = "world!";
var str3 = " Have a nice day!";
var res = str1.concat (str2, str3); // -->Hello world! Have a nice day!
str.trim()
문자열을 양쪽 끝 공백을 제거
var Sample = " Hello "
console.log(Sample.trim()) //"Hello"
\t : tab
\r : carriage return
\n : new line(줄 바꿈)
Template literals 안에서는 그냥 엔터 가능
str.match(regexp) : return an Array whose contents depend on the presence or absence of the global (g) flag, or null if no matches are found.
RegExp : regular expressions
<Using indexOf() to count occurrences of a letter in a string>
const str = 'To be, or not to be, that is the question.'
let count = 0
let position = str.indexOf('e')
while (position !== -1) {
count++
position = str.indexOf('e', position + 1)
}
console.log(count) // displays 4
console.log('string text line 1\n' +
'string text line 2');
// "string text line 1
// string text line 2"
console.log(string text line 1 string text line 2
);
// "string text line 1
// string text line 2"
const five = 5;
const ten = 10;
console.log('Fifteen is ' + (five + ten) + ' and not ' + (2 * five + ten) + '.');
// "Fifteen is 15 and not 20."
Now, with template literals, you are able to make use of the syntactic sugar making substitutions like this more readable:
const five = 5;
const ten = 10;
console.log(Fifteen is ${five + ten} and not ${2 * five + ten}.
);
// "Fifteen is 15 and not 20."