문ìì´ ì ëë¡ ë¤ë£¨ê¸°
ì´ì ê¹ì§ 문ìì´ì 기ì´ë¥¼ ì´í´ë³´ììµëë¤. ì´ì ë¶í° - í ì¤í¸ 문ìì´ì ê¸¸ì´ ì°¾ê¸°, 문ìì´ í©ì¹ê¸° ë° ìª¼ê°ê¸° ë±ê³¼ ê°ì- ë´ì¥ë ë©ìë를 ì¬ì©íì¬ ë¬¸ìì´ìì ìíí ì ìë ì ì©í ìì ì ëí´ ìê°í´ ë´ ìë¤. 문ìì´ì í 문ì를 ë¤ë¥¸ 문ìë¡ ëì²´íë ë±ì ìì ì ìíí©ëë¤.
| íìí ì¬ì ì§ì: | 기본 ì»´í¨í° íì© ë¥ë ¥, HTML ë° CSSì ëí 기본ì ì¸ ì´í´, JavaScriptê° ë¬´ìì¸ì§ ì´í´í©ëë¤. |
|---|---|
| 목í: | 문ìì´ì´ ê°ì²´ìì ì´í´íê³ , í´ë¹ ê°ì²´ìì ì¬ì©í ììë ëª ê°ì§ 기본 ë©ìë를 ì¬ì©íì¬ ë¬¸ìì´ì ì¡°ìíë ë°©ë²ì ë°°ìëë¤. |
Strings as objects
ì´ì ì ë§íì§ë§, ë¤ì ë§íë©´ - JavaScriptì 모ë ê²ì´ ê°ì²´ì ëë¤. 문ìì´ì ë§ë¤ ë, ì를 ë¤ë©´
var string = "This is my string";
ë³ìê° ë¬¸ìì´ ê°ì²´ ì¸ì¤í´ì¤ëë©´, ê²°ê³¼ì ì¼ë¡ ìë§ì ìì±ê³¼ ë©ìëê° ì¬ì© ê°ë¥íê² ë©ëë¤. String ê°ì²´ íì´ì§ë¡ ì´ëíì¬ íì´ì§ 측면ì 목ë¡ì ë´ë ¤ë¤ ë³´ë©´ ì´ê²ì ë³¼ ì ììµëë¤!
ì´ì ëê° ë ¹ê¸° ììí기 ì ì, ê±±ì íì§ ë§ììì¤! íìµ ì¬í ì¤ì ì´ê¸°ì ëë¶ë¶ì ì íìê° ììµëë¤. ê·¸ë¬ë ì¬ê¸°ìì ì´í´ë³¼ ëª ê°ì§ ì¬íì ì주 ì¬ì©íê² ë ê²ì ëë¤.
ì½ìì ìì 를 ìì±í´ ë³´ì¸ì. ìëì í ê°ì§ë¥¼ ì ê³µí©ëë¤(ì íì´ë ì ì°½ìì ì½ìì ì´ ì ìê³ , ë¸ë¼ì°ì ì ê°ë°ì ì½ìì ì¬ì©í ìë ììµëë¤).
문ìì´ì ê¸¸ì´ ì°¾ê¸°
ê°ë¨í©ëë¤ â ê°ë¨íê² length íë¡í¼í°ë¥¼ ì¬ì©í ì ììµëë¤. ë¤ì ì½ë를 ì
ë ¥í´ ë³´ì¸ì.
var browserType = "mozilla";
browserType.length;
ê²°ê³¼ë 7ì 리í´í´ì¼ í©ëë¤. 'mozilla'ë 7ê¸ìì´ê¸° ë문ì ëë¤. ì´ê²ì ì¬ë¬ ê°ì§ ì´ì ë¡ ì ì©í©ëë¤. ì를 ë¤ì´ ì´ë¦ì 길ì´ì ë°ë¼ ì´ë¦ì ìì를 ì ë ¬í´ì¼ íëê°, ì ì ê° ìì±í ì´ë¦ì´ í¹ì ê¸¸ì´ ì´ìì¼ ë ë무 길ë¤ë ê²ì ìë ¤ì¤ì¼ íë ê²½ì°ì ì¬ì©í ì ììµëë¤.
í¹ì 문ìì´ ì°¾ê¸°
ê´ë ¨íì¬, ëê´í¸ í기ë²ì ì´ì©í´ì 문ìì´ ìì 문ì를 구í ì ììµëë¤. ëê´í¸ í기ë²ì ë³ìëª ëì ëê´í¸ë¥¼ í¬í¨í©ëë¤. ëê´í¸ ììë 구íê³ ì¶ì 문ìì ì«ì를 í¬í¨ìí¤ë©´ ëë©°, ì를 ë¤ì´ ìëì ê²½ì° ì²« ë²ì§¸ 문ì를 구í ì ììµëë¤:
browserType[0];
ì»´í¨í°ë 1ì´ ìëë¼ 0ë¶í° ì«ì를 ì
ëë¤! 문ìì´ì ë§ì§ë§ 문ì를 구í기 ìí´ì, ì°ë¦¬ë ë¤ì ì½ë를 ì¬ì©í ì ìì¼ë©°, 기ì ì ì¸ length íë¡í¼í°ê³¼ ê°ì´ ì¬ì©íë©´ ìëì ê°ìµëë¤:
browserType[browserType.length - 1];
"mozilla"ë 7ê¸ìì´ì§ë§, ì«ìë 0ë¶í° ììí기 ë문ì ê¸ìì ìì¹ë 6ì
ëë¤. ê·¸ë 기 ë문ì length-1ì ì¬ì©í©ëë¤. ì를 ë¤ì´, ì¬ë¬ 문ìì´ ì¤ ì²« ë²ì§¸ 문ì를 ì°¾ì ìíë²³ìì¼ë¡ ì ë ¬í´ì¼ í ê²½ì°ì ì¬ì©í ì ììµëë¤.
문ìì´ ë´ë¶ì íì 문ìì´ ì°¾ê¸° ë° ì¶ì¶
-
ëëë¡ í° ë¬¸ìì´ ìì ìì 문ìì´(ì°ë¦¬ë ì´ê²ì íì 문ìì´ì´ë¼ê³ ì´ì¼ê¸° íë¤.)ì ì°¾ê³ ì¶ì ê²ì ëë¤. ì´ ìì ì
indexOf()를 ì¬ì©íì¬ ìë£í ì ììµëë¤, which takes a single parameter â 찾기 ìíë íì 문ìì´ì ì°¾ì ì ììµëë¤. ìëí´ ë´ ìë¤:jsbrowserType.indexOf("zilla");ê²°ê³¼ë 2ì ëë¤. íì 문ìì´ì¸ "zilla"ë "mozilla'ì 2ë² ìì¹(0, 1, 2â ê·¸ë¬ë¯ë¡ 3ë²ì§¸ 문ìì´)ìì ììí©ëë¤. ì´ë¬í ì½ëë 문ìì´ì íí°ë§íë ë° ì¬ì©ë ì ììµëë¤. ì를 ë¤ì´ ì¹ ì£¼ì 목ë¡ìì "mozilla"ê° í¬í¨ë 주ìë§ ì¸ìíê³ ì¶ì ê²½ì°ì ëë¤.
-
ë¤ë¥¸ ë°©ë²ì¼ë¡ë í ì ìì¼ë©°, ëì± í¨ì¨ì ì¼ ì ììµëë¤. ë¤ì ìì 를 ë°ë¼í´ ë´ ìë¤:
jsbrowserType.indexOf("vanilla");ì´ë ê² íë©´ -1( íì 문ìì´ (ì´ ê²½ì° 'vanilla')ì´ ê¸°ë³¸ 문ìì´ìì ë°ê²¬ëì§ ìì¼ë©´ ë°ííë¤.)ì 결과를 ì»ì ì ììµëë¤.
íì 문ìì´ 'mozilla'ê° í¬í¨ëì§ ìì 문ìì´ì 모ë ì¸ì¤í´ì¤ë¥¼ ì°¾ì¼ë ¤ë©´ ì´ ì°ì°ì를 ì¬ì©íê³ ìëì íìë ê²ì²ë¼ ë¶ì ì°ì°ì를 ì¬ì©í´ì ìì ì ìíí ì ììµëë¤. ë¤ìê³¼ ê°ì´ í ì ììµëë¤:
jsif (browserType.indexOf("mozilla") !== -1) { // do stuff with the string } -
문ìì´ ë´ìì ë¶ë¶ 문ìì´ì´ ì´ëìì ììëê³ ì´ë¤ 문ìë¡ ëëëì§ ìê³ ì¶ì¼ë©´
slice()를 ì¬ì©íì¬ ë¬¸ìì´ì ì¶ì¶í ì ììµëë¤. ë¤ìì ìëí´ ë´ ìë¤:jsbrowserType.slice(0, 3);"moz"를 ë°íí©ëë¤ - 첫 ë²ì§¸ íë¼ë©í°ë ì¶ì¶ì ììí 문ì ìì¹ì´ê³ ë ë²ì§¸ íë¼ë©í°ë ì¶ì¶í 문ìì ê°¯ìì ëë¤. ë°ë¼ì ì¬ë¼ì´ì¤ë 첫 ë²ì§¸ ìì¹ììë¶í° ì¸ ë²ì§¸ ìì¹ê¹ì§ í¬í¨ë©ëë¤.
-
ëí í¹ì 문ì ë¤ì 문ìì´ì ëë¨¸ì§ ë¬¸ì를 모ë ì¶ì¶íë ¤ë ê²½ì° ë ë²ì§¸ ë§¤ê° ë³ì를 í¬í¨íì§ ìê³ ë¬¸ìì´ìì ëë¨¸ì§ ë¬¸ì를 ì¶ì¶í ìì¹ì 문ì ìì¹ë§ í¬í¨íë©´ ë©ëë¤. ë¤ìì ìëí´ë³´ììì¤.
jsbrowserType.slice(2);ì´ë ê² íë©´ "zilla"ê° ë°íë©ëë¤. 문ìì 2ë²ì§¸ ìì¹ë "z"ì´ê³ ë ë²ì§¸ ë§¤ê° ë³ì를 í¬í¨íì§ ìì기 ë문ì ë°íë íì 문ìì´ì 문ìì´ì ëë¨¸ì§ ë¬¸ì 모ëì ëë¤.
ì°¸ê³ :
slice() has other options too; study the slice() page to see what else you can find out.
ë/ì문ì ë³ê²½
문ìì´ ë©ìë toLowerCase() ìtoUpperCase() ë 문ìì´ì ê°ì ¸ì ê·¸ê²ì 모ë ê°ê° ë문ìë ì문ìë¡ ë°ê¿ëë¤. ì´ë ë°ì´í°ë² ì´ì¤ì ì ì¥í기 ì ì 모ë ì¬ì©ì ì
ë ¥ ë°ì´í°ë¥¼ íì¤ííë ¤ë ê²½ì° ì ì©í©ëë¤.
ë¤ì íì ì ë ¥íì¬ ì´ë»ê² ëëì§ ì´í´ë³´ê² ìµëë¤.
var radData = "My NaMe Is MuD";
radData.toLowerCase();
radData.toUpperCase();
문ìì´ì ì¼ë¶ë¥¼ ë³ê²½í기
문ìì´ ë´ì í íì 문ìì´ì replace() 를 íµí´ ë¤ë¥¸ íì 문ìì´ë¡ ë°ê¿ ì ììµëë¤. ì´ ìì
ì 기본ì ì¸ ìì¤ìì ë§¤ì° ê°ë¨íê² ìëí©ëë¤. íì§ë§ ìì§ ìëí´ë³´ì§ ìì ê³ ê¸ ìì
ë ììµëë¤.
ê·¸ê²ì 2ê°ì 매ê°ë³ì를 ê°ì§ëë¤. â ë°ëì ë¹íë 문ìì ë°ê¾¸ë ¤ë 문ìì ëë¤. ë¤ì ìì 를 ë°ë¼í´ë³´ì¸ì.:
browserType.replace("moz", "van");
ìì
ì´ ì¹ì ììë 문ìì´ì ë¤ë£¨ë ë°©ë²ì ì¤ëª í©ëë¤. ìëì ê° ì¤ìµììë 문ìì´ë¡ ì´ë£¨ì´ì§ ë°°ì´ì 루í문ì ì¬ì©í´ bullet list(ë¶ë¦¿ 리ì¤í¸)ë¡ íííììµëë¤. ì§ê¸ ë°°ì´ì´ë 루í를 ì´í´í íìê° ììµëë¤. - ì´ë¬í ë´ì©ì ì¶íì ì¤ëª í©ëë¤. ì¤ìíê²ì ê°ê°ì 문ìì´ì´ ì°ë¦¬ê° ìíë íìì¼ë¡ ì¶ë ¥íë ì½ë를 ìì±íë ê²ì ëë¤.
ê° ìì ìë 리ì ë²í¼ì´ ìê³ , 리ì ë²í¼ì ì¤ì를 íê±°ë ì½ëê° ìëíì§ ììì ì¬ì¤ì íëë° ì¬ì©í ì ììµëë¤. í´ê²° ë°©ë²ì 모를 ë, í´ëµ ë²í¼(solution button)ì ë르면 í´ëµì ë³¼ ì ììµëë¤.
ì¸ì¬ë§ íí°ë§ í기
첫 ë²ì§¸ ìì ë ê°ë¨í ììí´ë´ ìë¤. ì°ë¦¬ë ë°°ì´ì ë¤ì´ìë í¬ë¦¬ì¤ë§ì¤ ì¸ì¬ë§ ë©ìì§ë¥¼ ì ë ¬íë ¤ê³ í©ëë¤. if(...)ì ì¬ì©í´ ê° ë¬¸ìì´ì ë¹êµíê³ í¬ë¦¬ì¤ë§ì¤ ë©ìì§ì¸ ê²½ì°ì 목ë¡ë§ ì¸ìíë ¤ê³ í©ëë¤.
- 먼ì ê° ë©ìì§ê° í¬ë¦¬ì¤ë§ì¤ ë©ìì§ì¸ì§ ì¬ë¶ë¥¼ í ì¤í¸í ì ìë ë°©ë²ì ìê°í´ë´ ìë¤. ë©ìì§ë¤ì ì´ë¤ 문ìì´ì´ ìê³ , ì¡´ì¬íëì§ í ì¤í¸í기 ìí´ ì´ë¤ ë°©ë²ì ì¬ì©í ì ììê¹ì?
- ì°ì°ìì í¼ì°ì°ì를 ì¬ì©í´ 조건문ì ë§ë¤ì´ì¼ í©ëë¤. ì°ì°ì ì¼ìª½ì ìëê²ê³¼ ì°ì°ì ì¤ë¥¸ìª½ì ìë ê²ì´ ëë±íê°ì? ëë ì´ ê²½ì° ì¼ìª½ ë©ìëê° ì¤ë¥¸ìª½ì¼ë¡ ê²°ê³¼ê°ì ì ë¬í©ëê¹?
- íí¸ : ì´ ê²½ì° ë©ìë í¸ì¶ì´ ê²°ê³¼ê°ê³¼ ê°ì§ ììì§ í ì¤í¸íë ê²ì´ ë ì ì©í ì ììµëë¤.
ë/ì문ì ë§ê² ìì í기
ì´ ìì ìë ìêµ ëìì ì´ë¦ë¤ì 모ìë¨ìµëë¤ë§ ë/ì문ìê° ì못ëì´ ììµëë¤. ì°ë¦¬ë ì´ ë¬¸ìë¤ì 첫 ë²ì§¸ 문ì를 ì ì¸íê³ ëª¨ë ì문ìë¡ ë³ê²½í´ì¼ í©ëë¤. ì´ê²ì ë¤ìê³¼ ê°ì ë°©ìì¼ë¡ í ì ììµëë¤:
inputë³ìì ë´ê¸´ 문ìì´ ì 체를 ì문ìë¡ ë³íí í ìë¡ì´ ë³ìì ì ì¥íì¸ì.- ìë¡ì´ ë³ìì ì ì¥ë 문ìì´ì 첫 문ì를 ë¤ë¥¸ ë³ìì ì ì¥íì¸ì
- Using this latest variable as a substring, replace the first letter of the lowercase string with the first letter of the lowercase string changed to upper case. Store the result of this replace procedure in another new variable.
- Change the value of the
resultvariable to equal to the final result, not theinput.
ì°¸ê³ : A hint â the parameters of the string methods don't have to be string literals; they can also be variables, or even variables with a method being invoked on them.
Making new strings from old parts
In this last exercise the array contains a bunch of strings containing information about train stations in the North of England. The strings are data items that contain the three letter station code, followed by some machine-readable data, followed by a semi-colon, followed by the human-readable station name. For example:
MAN675847583748sjt567654;Manchester Piccadilly
We want to extract the station code and name, and put them together in a string with the following structure:
MAN: Manchester Piccadilly
We'd recommend doing it like this:
- Extract the three-letter station code and store it in a new variable.
- Find the character index number of the semi-colon.
- Extract the human-readable station name using the semi-colon character index number as a reference point, and store it in a new variable.
- Concatenate the two new variables and a string literal to make the final string.
- Change the value of the
resultvariable to equal to the final string, not theinput.
ê²°ë¡
JavaScriptìì 문ì¥ê³¼ ë¨ì´ë¤ì ë¤ë£° ì ìë íë¡ê·¸ëë° ë¥ë ¥ì´ ë§¤ì° ì¤ìíë¤. ì¹ì¬ì´í¸ë ì¬ëë¤ê³¼ ìíµíë ê³µê°ì´ê¸° ë문ì´ë¤. ì´ ë¬¸ìë 문ìì´ì ë¤ë£° ì ìë 기ì´ì ì¸ ë´ì©ì ëí´ ë¤ë£¨ìë¤. ì´ ë´ì©ì ìì¼ë¡ ë°°ì°ê² ë ì¬í ê³¼ì ì ëìì´ ë ê²ì´ë¤. ë¤ìì¼ë¡ ë°°ì´ì ëí´ ììë³´ê² ë¤.