본문 바로가기
프로그래밍/JavaScript

JavaScript 문자열 객체

by zoo10 2013. 1. 4.

JavaScript Strings

string은 "John Doe"와 같은 단어들의 조합이다.


Example

var carname="Volvo XC60";
var carname='Volvo XC60';


index를 이용해서 각 문자들에 접근할 수 있다.

Example

var character=carname[7];
String 인덱스는 zero-based이다. 이 뜻은 [0] 이 첫번째 문자, [1] 이 두번째 문자라는 것을 뜻한다.


따옴표 표현은 아래와 같이 할 수 있다.

Example

var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';

또는 \ escape 문자를 이용하여 표현할 수도 있다.

Example

var answer='It\'s alright';
var answer="He is called \"Johnny\"";

Try it yourself »


String Length

문자의 길이는 length 속성으로 알수 있다.


Example

var txt="Hello World!";
document.write(txt.length);

var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write(txt.length);

Try it yourself »

Finding a String in a String

문자열이 포함되었는지 확인하기 위해 indexOf() 함수를 사용할 수 있다. 이 함수는 첫번째로 찾은 위치의 인덱스를 반환해 준다.

Example

var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");
Try it yourself »


일치하는 문자가 없으면 -1를 반환한다.

lastIndexOf() 함수를 사용하면 뒤에서 부터 검색할 수 있다.



Matching Content

match() 함수는 일치하는 문자가 있는지 확인할 수 있다.


Example

var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));

Try it yourself »


Replacing Content

replace() 함수는 다른 문자로 기존 문자를 바꿀 수 있다.

Example

str="Please visit Microsoft!"
var n=str.replace("Microsoft","W3Schools");

Try it yourself »


Upper Case and Lower Case

대소문자를 변환하고 싶다면 toUpperCase() / toLowerCase() 함수를 사용할 수 있다.


Example

var txt="Hello World!";       // String
var txt1=txt.toUpperCase();   // txt1 is txt converted to upper
var txt2=txt.toLowerCase();   // txt2 is txt converted to lower

Try it yourself »


Convert a String to an Array

string.split():은 문자열을 특정 문자로 구분하여 배열로 바꿀 수 있다.

Example

txt="a,b,c,d,e"   // String
txt.split(",");   // Split on commas
txt.split(" ");   // Split on spaces
txt.split("|");   // Split on pipe 

Try it yourself »


Special Characters

backslash (\) 는 따옴표 사용, 줄바꿈, 특수 문자를 사용해야 할때 사용된다.


아래 JavaScript 코드를 보자:

var txt="We are the so-called "Vikings" from the north.";
document.write(txt);

이 JavaScript는, 따옴표 사용이 잘못되어 오류를 발생시킨다. 이런 문제를 해결하려면 backslash (\)를 사용해야 한다. 

var txt="We are the so-called \"Vikings\" from the north.";
document.write(txt);

JavaScript 코드는 정상처리 된다.

아래는 특수문자에 대한 설명이다.

CodeOutputs
\'single quote
\"double quote
\\backslash
\nnew line
\rcarriage return
\ttab
\bbackspace
\fform feed


String Properties and Methods

String Object Properties

PropertyDescription
constructor생성자
length문자열 길이를 반환
prototype사용자 속성과 함수 추가

String Object Methods

MethodDescription
charAt()인덱스 인자에 해당하는 문자를 반환
charCodeAt()인덱스 인자에 해당하는 유니코드 문자를 반환
concat()두 개 이상의 문자열을 합침
fromCharCode()문자를 유니코드로 변환
indexOf()문자열에서 지정한 값의 첫번째 발견 위치를 반환
lastIndexOf()문자열에서 뒤에서 부터 지정한 값의 첫번째 발견 위치를 반환
match()문자열과 정규식 표현이 일치하는 부분 반환
replace()문자열내의 지정한 값을 찾아서 신규 값으로 변경
search()문자열에서 정규식 표현으로 지정한 값의 첫번째 발견 위치를 반환
slice()지정한 범위 내의 문자열을 잘라내어 새로운 문자열을 생성
split()지정한 문자를 기준으로 잘라내어 문자 배열을 생성
substr()지정한 범위 내의 문자열을 잘라내어 새로운 문자열을 생성
substring()지정한 범위 내의 문자열을 잘라내어 새로운 문자열을 생성
toLowerCase()글자를 소문자로 변환
toUpperCase()글자를 대문자로 변환
valueOf()String 객체의 원시 값을 반환

'프로그래밍 > JavaScript' 카테고리의 다른 글

JavaScript Navigator 객체  (0) 2013.01.05
JavaScript 브라우저 객체 모델 BOM  (2) 2013.01.05
JavaScript 수학 객체 Math  (0) 2013.01.05
JavaScript 논리 객체, boolean  (0) 2013.01.05
JavaScript 반복문 For  (0) 2013.01.04
JavaScript 에러 던지기 Throw  (0) 2013.01.04
JavaScript 런타임 에러 제어 Try Catch  (0) 2013.01.04
JavaScript 이벤트  (0) 2013.01.04