본문 바로가기
개발언어

javascript substring() method 문자열 추출함수

by 엔돌슨 2008. 3. 5.
반응형

javascript에서 문자열 추출함수를 찾다가 좋은걸 찾았다.
그림설명까지 ~ 추출방법은 substring( 시작, 끝)
아주 간단하다.

이것도 문자열 추출함수 substr이다
http://freewebdesigntutorials.com/javaScriptTutorials/jsStringObject/substrMethod.htm

그리고 역시나 래퍼런스를 참조하면 더 많이 알수있다.
http://www.w3schools.com/jsref/jsref_obj_string.asp

JavaScript String Object Reference

String Object Methods

FF: Firefox, N: Netscape, IE: Internet Explorer

Method Description FF N IE
anchor() Creates an HTML anchor 1 2 3
big() Displays a string in a big font 1 2 3
blink() Displays a blinking string 1 2  
bold() Displays a string in bold 1 2 3
charAt() Returns the character at a specified position 1 2 3
charCodeAt() Returns the Unicode of the character at a specified position 1 4 4
concat() Joins two or more strings 1 4 4
fixed() Displays a string as teletype text 1 2 3
fontcolor() Displays a string in a specified color 1 2 3
fontsize() Displays a string in a specified size 1 2 3
fromCharCode() Takes the specified Unicode values and returns a string 1 4 4
indexOf() Returns the position of the first occurrence of a specified string value in a string 1 2 3
italics() Displays a string in italic 1 2 3
lastIndexOf() Returns the position of the last occurrence of a specified string value, searching backwards from the specified position in a string 1 2 3
link() Displays a string as a hyperlink 1 2 3
match() Searches for a specified value in a string 1 4 4
replace() Replaces some characters with some other characters in a string 1 4 4
search() Searches a string for a specified value 1 4 4
slice() Extracts a part of a string and returns the extracted part in a new string 1 4 4
small() Displays a string in a small font 1 2 3
split() Splits a string into an array of strings 1 4 4
strike() Displays a string with a strikethrough 1 2 3
sub() Displays a string as subscript 1 2 3
substr() Extracts a specified number of characters in a string, from a start index 1 4 4
substring() Extracts the characters in a string between two specified indices 1 2 3
sup() Displays a string as superscript 1 2 3
toLowerCase() Displays a string in lowercase letters 1 2 3
toUpperCase() Displays a string in uppercase letters 1 2 3
toSource() Represents the source code of an object 1 4 -
valueOf() Returns the primitive value of a String object 1 2 4


String Object Properties

Property Description FF N IE
constructor A reference to the function that created the object 1 4 4
length Returns the number of characters in a string 1 2 3
prototype Allows you to add properties and methods to the object 1 2 4



This tutorial introduces the substring() method, from the JavaScript String object. The substring() method is designed to extract a substring of text from another string, while not modifying the original string. The substring() method takes at least one argument (to take a substring ending with the end of the original string), and an optional second argument indicating the position index at which the extracted substring should stop.

Related string method:
The substr() method extracts a substring from a fixed start index and relative string length.

Using the substring() Method with One Argument

In the script below, we will extract a substring using only one argument:

var carter = "I learn JavaScript.";
alert( carter.substring(8) );

By only passing one argument (the start position index), the substring() method extracts characters until it reaches the end of the string on which it is applied: JavaScript substring() method with 1 argument

In this respect, the substring() method behaves exactly like the substr() method, when only the start position index is passed.

Using the substring() Method with Two Arguments

The substring() method accepts a second optional argument: it indicates the point at which it should stop extracting characters (in other words, the second argument indicates the end position index).

var carter = "I learn JavaScript.";
alert( carter.substring(8,12) );

JavaScript's charAt(), substr(), and substring() Methods

The JavaScript string objects includes three methods that allow you to extract substrings of text: the charAt() method (for a single character), the substr() method (fixed start index and relative length), and the substring() method (for a fixed start and end index positions).

The script below how to obtain the same result from each of the three methods:

var carter = "I learn JavaScript.";
// Let's extract the "J" in "JavaScript"
var outCode;
outCode  = carter.charAt(8) +"\n";
outCode += carter.substr(8,1) +"\n";
outCode += carter.substring(8,9);
alert( outCode );

After storing the results, separated by a new line character, the last line of code produces the following result: Comparing JavaScript's charAt(), substr(), and substring() methods

Test the substring() Method

Interactively test the substring() method by editing the JavaScript code below and clicking the Test substring() Method button.