API - String, StringBuffer, StringBuilder

  • java.lang

ํด๋ž˜์Šค

์šฉ๋„

String

๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ์–ป์„ ๋•Œ ์‚ฌ์šฉ

StringBuffer, StringBuilder

๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ๋‚ด๋ถ€ ๋ฌธ์ž์—ด์„ ์กฐ์ž‘ํ•  ๋•Œ ์‚ฌ์šฉ

String ํด๋ž˜์Šค

์ž๋ฐ”์˜ ๋ฌธ์ž์—ด์€ String ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋กœ ๊ด€๋ฆฐ๋œ๋‹ค. String ํด๋ž˜์Šค์˜ ๋‹ค์–‘ํ•œ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด์„œ ์ง์ ‘ String๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฆฌํ„ด ํƒ€์ž…

๋ฉ”์†Œ๋“œ๋ช…(๋งค๊ฐœ ๋ณ€์ˆ˜)

์„ค๋ช…

char

charAt(int index)

ํŠน์ • ์œ„์น˜์˜ ๋ฌธ์ž ๋ฆฌํ„ด

boolean

equals(Object anObject)

๋‘ ๋ฌธ์ž์—ด์„ ๋น„๊ต

byte[]

getBytes()

byte[]๋กœ ๋ฆฌํ„ด

byte[]

getBytes(charset charset)

๋ฌธ์ž์—ด ๋‚ด์—์„œ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ์œ„์น˜๋ฅผ ๋ฆฌํ„ด

int

length()

์ด ๋ฌธ์ž์ˆ˜๋ฅผ ๋ฆฌํ„ด

String

replace(CharSequence target, CharSequence replacement)

target๋ถ€๋ถ„์„ replacement๋กœ ๋Œ€์ฒดํ•œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๋ฆฌํ„ด

String

substring(int beginIndex)

beginIndex ์œ„์น˜์—์„œ ๋๊นŒ์ง€ ์ž˜๋ผ๋‚ธ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๋ฆฌํ„ด

String

substring(int beginIndex, int endIndex)

beginIndex ์œ„์น˜์—์„œ endIndex๊นŒ์ง€ ์ž˜๋ผ๋‚ธ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๋ฆฌํ„ด

String

toLowerCase()

์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๋ฆฌํ„ด

String

toUpperCase()

์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๋ฆฌํ„ด

String

trim()

์•ž๋’ค ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๋ฆฌํ„ด

String

valueOf(int i) valueOf(double d)

๊ธฐ๋ณธ ํƒ€์ž… ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ด

์‚ฌ์šฉ ๋นˆ๋„๊ฐ€ ๋†’์€ ์ƒ์„ฑ์ž

๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋Š” ๋ณดํ†ต byte[] ๋ฐฐ์—ด์ด๋ฏ€๋กœ ์ด๊ฒƒ์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

//๋ฐฐ์—ด ์ „์ฒด๋ฅผ String ๊ฐ์ฒด๋กœ ์ƒ์„ฑ
String str = new String(byte[] bytes);
//์ง€์ •ํ•œ ๋ฌธ์ž์…‹์œผ๋กœ ๋””์ฝ”๋”ฉ
String str = new String(byte[] bytes, String charsetName);

//๋ฐฐ์—ด์˜ offset ์ธ๋ฑ์Šค ์œ„์น˜๋ถ€ํ„ฐ length๋งŒํผ String ๊ฐ์ฒด ์ƒ์„ฑ
String str = new String(byte[] bytes,int offset, int length);
// ์ง€์ •ํ•œ ๋ฌธ์ž์…‹์œผ๋กœ ๋””์ฝ”๋”ฉ
String str = new String(byte[] bytes, ,int offset, int length,String charsetName);
public static void main(String[] args){
    byte[] bytes = {72, 101, 108, 111, 32, 74, 97, 118, 97};

    String str1 = new String(bytes);
    System.out.println(str1); //=> Hello Java
    String str2 = new String(bytes,6,4);
    System.out.println(str2); //=>Java
}

ํ‚ค๋ณด๋“œ๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฌธ์ž์—ด์—๋Š” ์—”ํ„ฐํ‚ค๋ฅผ ๋ˆŒ๋ €๋‹ค๋ฉด \r, \n์˜ ์ฝ”๋“œ๊ฐ’์ด ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์— ์ €์žฅ๋œ๋‹ค. ์˜์–ด๋Š” 1byte, ๋‹ค๋ฅธ ๋‚˜๋ผ ์–ธ์–ด๋Š” 2byte๋กœ ํ‘œํ˜„๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ž…๋ ฅ๋œ ๋ฌธ์ž ์ˆ˜์™€ ๋ฐ”์ดํŠธ ์ˆ˜๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

public static void main(String[] args){
    byte[] bytes = new byte[100];

    System.out.println("์ž…๋ ฅ : ");
    int readByteNo = System.in.read(bytes);
    String str2 = new String(bytes,0,readByteNo-2);
    System.out.println(str2);
}

๋ฌธ์ž์ถ”์ถœ(charAt())

๋งค๊ฐœ๊ฐ’์œผ๋กœ ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค์˜ ๋ฌธ์ž๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

String sub = "์ž๋ฐ” ํ”„๋กœ๊ทธ๋ž˜๋ฐ";
char charVal = sub.charAt(3);
//=>ํ”„

๋ฌธ์ž์—ด ๋น„๊ต(equals())

String val1 = new String("Java");
String val2 = "java";
va1.equals(val2);
//=>true

๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜(getBytes())

๋„คํŠธ์›Œํฌ๋กœ ๋ฌธ์ž์—ด์„ ์ „์†กํ•˜๊ฑฐ๋‚˜, ๋ฌธ์ž์—ด์„ ์•”ํ˜ธํ™”ํ•  ๋•Œ ๋ฌธ์ž์—ด์„ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

byte[] bytes = "๋ฌธ์ž์—ด".getBytes();

์‹œ์Šคํ…œ์˜ ๊ธฐ๋ณธ ๋ฌธ์ž์…‹์œผ๋กœ ์ธ์ฝ”๋”ฉ๋œ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•œ๋‹ค. ๋งŒ์•ฝ ํŠน์ • ๋ฌธ์ž์…‹์œผ๋กœ ์ธ์ฝ”๋”ฉ๋œ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์„ ์–ป์œผ๋ ค๋ฉด ๋‹ค์Œ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด๋œ๋‹ค.

byte[] bytes = "๋ฌธ์ž์—ด".getBytes(Charset charset);
byte[] bytes = "๋ฌธ์ž์—ด".getBytes("UTF-8");

์—ฌ๊ธฐ์„œ EUC-KR์€ ํ•œ๊ธ€ 2byte, UTF-8์€ 3byte๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

์‹œ์Šคํ…œ ๊ธฐ๋ณธ ๋ฌธ์ž์…‹๊ณผ ๋‹ค๋ฅธ ๋ฌธ์ž์…‹์œผ๋กœ ์ธ์ฝ”๋”ฉ๋œ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์ผ ๊ฒฝ์šฐ ๋‹ค์Œ String ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด ๋””์ฝ”๋”ฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

String str = new String(byte[] bytes,String charsetName);

getBytes(Charset charset) ๋ฉ”์†Œ๋“œ๋Š” ์ž˜๋ชป๋œ ๋ฌธ์ž์…‹์„ ๋งค๊ฐœ๊ฐ’์œผ๋กœ ์ค„ ๊ฒฝ์šฐ UnsupportedEncodingException์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

try{
    byte[] bytes = "๋ฌธ์ž์—ด".getBytes("UTF-8");
}catch(UnsupportedEncodingException e){
    e.printStackTrace();
}

๋ฌธ์ž์—ด ์ฐพ๊ธฐ(indexOf())

๋งค๊ฐœ๊ฐ’์œผ๋กœ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์‹œ์ž‘๋˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ๋งŒ์•ฝ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€์•Š์œผ๋ฉด -1์„ ๋ฆฌํ„ดํ•œ๋‹ค.

String sub = "java programming";
int index = sub.indexOf("programming");
//=>5
if(๋ฌธ์ž์—ด.indexOf("์ฐพ๋Š” ๋ฌธ์ž์—ด")!=-1){
    //ํฌํ•จ๋œ ๊ฒฝ์šฐ
}else{
    //ํฌํ•จ๋˜์–ด์žˆ์ง€์•Š์€ ๊ฒฝ์šฐ
}

๋ฌธ์ž์—ด ๊ธธ์ด(length())

๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

String sub = "java programming";
int length = sub.length();
//=>16

๋ฌธ์ž์—ด ๋Œ€์น˜(replace())

์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๊ฐ’์ธ ๋ฌธ์ž์—ด์„ ์ฐพ์•„ ๋‘๋ฒˆ์งธ ๋งค๊ฐœ๊ฐ’์ธ ๋ฌธ์ž์—ด๋กœ ๋Œ€์น˜ํ•œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜๊ณ  ๋ฆฌํ„ดํ•œ๋‹ค.

String old = "ruby programming";
String new = old.replace("ruby","java");
//=>java programming

๋ฌธ์ž์—ด ์ž˜๋ผ๋‚ด๊ธฐ(substring())

์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค์—์„œ ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•œ๋‹ค.

String old = "java programming";
String first = old.substring(0,4);
//=>java
String seconde = old.substring(5);

์ธ๋ฑ์Šค ํฌํ•จ ๋ฒ”์œ„๋Š” startIndex<= new < endIndex์ด๋‹ค.

์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž ๋ณ€๊ฒฝ(toLowerCase(),toUpperCase())

toLowerCase()๋Š” ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝ, toUpperCase()๋Š” ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.

String str = "Java Programming";
str.toLowercase();
//=>"java programming"
str.toUppercase();
//=>"JAVA PROGRAMMING"

์ด๋•Œ ๋Œ€์†Œ๋ฌธ์ž ๊ด€๊ณ„์—†์ด ๊ฐ™์€์ง€ ๋น„๊ตํ•ด์ฃผ๋ ค๋ฉด equalsIgnoreCase() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

str1.equalsIgnoreCase(str);

๋ฌธ์ž์—ด ์•ž๋’ค ๊ณต๋ฐฑ ์ž˜๋ผ๋‚ด๊ธฐ(trim())

๋ฌธ์ž์—ด์˜ ์•ž๋’ค ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜๊ณ  ๋ฆฌํ„ดํ•œ๋‹ค.

String str = "       Java Programming        ";
str.trim();
//=>"Java Programming"

trim์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ด์„œ ์›๋ž˜ ๋ฌธ์ž์—ด์˜ ๊ณต๋ฐฑ์ด ์ œ๊ฑฐ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด ๋ณ€ํ™˜(valueOf())

๊ธฐ๋ณธ ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž… ๋ณ„๋กœ ์˜ค๋ฒ„๋กœ๋”ฉ ๋˜์–ด์žˆ๋‹ค.

static String valueOf(boolean b)
static String valueOf(char c)
static String valueOf(int i)
static String valueOf(long l)
static String valueOf(double d)
static String valueOf(float f)

StringBuffer, StringBuilder ํด๋ž˜์Šค

๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๋Š” String์€ ๋‚ด๋ถ€์˜ ๋ฌธ์ž์—ด์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ String์˜ replace() ๋ฉ”์†Œ๋“œ๋Š” ๋Œ€์น˜๋œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์ด์ง€ ๋‚ด๋ถ€ ๋ฌธ์ž์—ด์„ ๋Œ€์น˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

+ ์—ฐ์‚ฐ์—๋„ ๋งˆ์ฐฌ๊ฐ€์ง€ ์ด๋‹ค.

String str1 = "abc";
str1+="defg";

๋Š” "abc"๊ฐ์ฒด๋Š” ๊ทธ๋Œ€๋กœ ์žˆ๊ณ , "abcdefg"์˜ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ ํ›„ ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋œ๋‹ค.

๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•˜๋Š” + ์—ฐ์‚ฐ์ž๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋ฉด ํ• ์ˆ˜๋ก ๊ทธ๋งŒํผ String ๊ฐ์ฒด์˜ ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ๊ทธ๋žจ ์„ฑ๋Šฅ์„ ๋Š๋ฆฌ๊ฒŒ ํ•˜๋Š” ์š”์ธ์ด ๋œ๋‹ค.

๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…์ด ๋งŽ์„ ๊ฒฝ์šฐ์—๋Š” String ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค StringBuffer, StringBuilde ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ด ๋‘ ํด๋ž˜์Šค๋Š” ๋‚ด๋ถ€ Buffer(:๋ฐ์ดํ„ฐ๋ฅผ ์ž„์‹œ๋กœ ์ €์žฅํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ)์— ๋ฌธ์ž์—ด์„ ์ €์žฅํ•ด๋‘๊ณ , ๊ทธ ์•ˆ์—์„œ ์ถ”๊ฐ€, ์ˆ˜์ •, ์‚ญ์ œ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์–ด ์žˆ๋‹ค. ์ฆ‰, String์ฒ˜๋Ÿผ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ ๋„ ๋ฌธ์ž์—ด์„ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

๋‘ ํด๋ž˜์Šค์˜ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ๋™์ผํ•˜๋‚˜ ์ฐจ์ด์ ์€ StringBuffer๋Š” ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋™๊ธฐํ™”๊ฐ€ ์ ์šฉ๋˜์–ด ์žˆ์–ด ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ์— ์•ˆ์ „(thread-safe)ํ•˜์ง€๋งŒ, StringBuilder ๋Š” ๋‹จ์ผ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค๊ณ„๋˜์–ด ์žˆ๋‹ค.

StringBuilder

// 16๊ฐœ ๋ฌธ์ž๋“ค์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์ดˆ๊ธฐ๋ฒ„ํผ
StringBuilder sb = new StringBuilder();
//์ฃผ์–ด์ง„ ๊ฐœ์ˆ˜๋งŒํผ ๋ฌธ์ž๋“ค์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์ดˆ๊ธฐ ๋ฒ„ํผ
StringBuilder sb = new StringBuilder(16);
// ์ฃผ์–ด์ง„ str๋งค๊ฐœ๊ฐ’์„ ๋ฒ„ํผ์˜ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์ €์žฅ
StringBuilder sb = new StringBuilder("Java");

๋ฒ„ํผ๊ฐ€ ๋ถ€์กฑํ•  ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ ๋ฒ„ํผ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐ ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋Š” ๊ทธ๋‹ค์ง€ ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค.

๋ฉ”์†Œ๋“œ

์„ค๋ช…

append(...)

๋ฌธ์ž์—ด ๋์— ์ฃผ์–ด์ง„ ๋งค๊ฐœ๊ฐ’์„ ์ถ”๊ฐ€

insert(...)

๋ฌธ์ž์—ด ์ค‘๊ฐ„์— ์ฃผ์–ด์ง„ ๋งค๊ฐœ๊ฐ’ ์ถ”๊ฐ€

delete(int start,int end)

๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ถ„ ์‚ญ์ œ

deleteCharAt(int index)

๋ฌธ์ž์—ด์—์„œ ์ฃผ์–ด์ง„ index์˜ ๋ฌธ์ž๋ฅผ ์‚ญ์ œ

replace(int start, int end, String str)

๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ถ„์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋Œ€์น˜

StringBuilder reverse()

๋ฌธ์ž์—ด์˜ ์ˆœ์„œ๋ฅผ ๋’ค๋ฐ”๊ฟˆ

setCharAt(int index, char ch)

๋ฌธ์ž์—ด์—์„œ ์ฃผ์–ด์ง„ index์˜ ๋ฌธ์ž๋ฅผ ๋‹ค๋ฅธ ๋ฌธ์ž๋กœ ๋Œ€์น˜

append์™€ insert ๋ฉ”์†Œ๋“œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋‹ค์–‘ํ•œ ํƒ€์ž…์œผ๋กœ ์˜ค๋ฒ„๋กœ๋”ฉ ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๋Œ€๋ถ€๋ถ„ ๊ฐ’์„ ๋ฌธ์ž๋กœ ์ถ”๊ฐ€, ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.

StringBuilder sb = new StringBuilder();

sb.append("JAVA ");
sb.append("Programming");
// ๋ฒ„ํผ์— ์žˆ๋Š” ๊ฒƒ์„ Stringํƒ€์ž…์œผ๋กœ ๋ฆฌํ„ด
sb.toString();

sb.insert(4,"2");
//=>JAVA2 Programming

sb.setCharAt(4,'6');
//=>JAVA6 Programming

sb.replace(6,17,"Book");
//=>JAVA6 Book

sb.delete(4,5);
//=>JAVA Book

StringBuffer

StringBuffer stringBuffer = new StringBuffer();
StringBuilder stringBuilder = new StringBuilder();

new Thread(() -> {
    for(int i=0; i<10000; i++) {
        stringBuffer.append(i);
        stringBuilder.append(i);
    }
}).start();

new Thread(() -> {
    for(int i=0; i<10000; i++) {
        stringBuffer.append(i);
        stringBuilder.append(i);
    }
}).start();

new Thread(() -> {
    try {
        Thread.sleep(5000);

        System.out.println("StringBuffer.length: "+ stringBuffer.length());
        System.out.println("StringBuilder.length: "+ stringBuilder.length());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}).start();
    StringBuffer.length: 77780
    StringBuilder.length: 76412

StringBuilder์™€ StringBuffer์˜ ๊ฒฐ๊ณผ ๊ฐ’์ด ๋‹ค๋ฅธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” thread๋“ค์ด ๋™์‹œ์— StringBuilder ํด๋ž˜์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์–ด ๋” ์ž‘์€ ์ˆ˜๊ฐ€ ๋‚˜์˜จ ๊ฒƒ์ด๋‹ค. String Buffer๋Š” multi-thread ํ™˜๊ฒฝ์—์„œ ๋‹ค๋ฅธ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ๋ชปํ•˜๋„๋ก ๋™๊ธฐํ™”(Synchronization)๋˜์–ด์žˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ Web์ด๋‚˜ Socket๊ณผ ๊ฐ™์ด ๋น„๋™๊ธฐ๋กœ ๋™์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์„๋•Œ๋Š” StringBuffer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•ˆ์ „ํ•˜๋‹ค.

์ฐธ์กฐ ๋ฌธ์„œ

Last updated