๐Ÿฎ[JAVA]StringBufferํด๋ž˜์Šค๐Ÿฎ

StringBufferํด๋ž˜์Šค

java.lang.StringBuffer ํด๋ž˜์Šค๋Š” ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํด๋ž˜์Šค๋กœ StringBuffer ํด๋ž˜์Šค๋กœ ์ƒ์„ฑํ•œ ๋ฌธ์ž์—ด์€ ๋‚ด์šฉ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ๋™์ ์œผ๋กœ ๋ฌธ์ž์—ด์˜ ๋‚ด์šฉ์„ ๋ฐ”๊พธ๊ฑฐ๋‚˜ ์œ„์น˜๋ฅผ ์กฐ์ •ํ•˜๋Š” ๋“ฑ์˜ ์ผ์„ ํ•  ๋Œ€ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.

๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ๋ฒ•

  • StringBuffer ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ˜•์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

    StringButter ๊ฐ์ฒด ๋ณ€์ˆ˜ = new StringBuffer(String str);

  • StringBuffer ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

    StringBuffer sb1 = new StringBuffer("Java");
    StringBuffer sb2 = new StringBufferI("Java");
    ..
    //๋ฉ”๋ชจ๋ฆฌ sb1 -> Java , sb2 -> Java

๋Œ€ํ‘œ์ ์ธ ๋ฉ”์†Œ๋“œ

๊ฐ€. append() ๋ฉ”์†Œ๋“œ

  • ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

  • ํ˜•์‹

    public StringBuffer append(๋งค๊ฐœ๋ณ€์ˆ˜)

  • ๋งค๊ฐœ๋ณ€์ˆ˜์—๋Š” String, StringBuffer, boolean, char, char[], double,float,long,Object ๋“ฑ์ด ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

  • ์˜ˆ์ œ

class AppendExam{
	public static void main(String args[]){
    	StringBuffer sb1 = new StringBuffer(" Java Programming");
        StringBuffer sb2 = new StringBuffer(" Project");
        StringBuffer str1 = null;
        StringBuffer str2 = null;
        str1 = sb1.append(" Project");
        str2 = sb1.append(sb2);
        System.out.println("sb1 = " +sb1);
        System.out.println("sb2 = " +sb2);
        System.out.println("str1 = " +str1);
        System.out.println("str2 = " +str2);
    }
}
        

์‹คํ–‰๊ฒฐ๊ณผ

Java Programming Project Project
Project
Java Programming Project Project
Java Programming Project Project


๋‚˜. replace()๋ฉ”์†Œ๋“œ

  • ๋ฌธ์ž์—ด์„ ์น˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
  • ํ˜•์‹

    public StringBuffer replace(int start, int end ,String str)

  • start ~ end-1์˜ ๋ฌธ์ž์—ด์„ str๋กœ ์น˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ (๋ฐ”๊พธ๋Š” ๋ฉ”์†Œ๋“œ)
  • ์˜ˆ์ œ
class ReplaceExam{
	public static void main(String args[]){
    		StringBuffer sb1 = new StringBuffer("java Programming");
            StringBuffer sb2 = null;
            sb2 = sb1.replace(0,4,"JAVA");
            System.out.println("sb2  = " + sb2);
      }
 }

์‹คํ–‰๊ฒฐ๊ณผ

JAVA Programming
//์‹œ์ž‘๋ถ€ํ„ฐ ๋๊นŒ์ง€์˜ ๋ถ€๋ถ„์„ ๋ฌธ์ž์—ด๋กœ ๊ต์ฒดํ•˜๋Š” ๊ฒƒ


๋‹ค. reverse()๋ฉ”์†Œ๋“œ

  • ๋ฌธ์ž์—ด์„ ์—ญ์œผ๋กœ(๊ฑฐ๊พธ๋กœ) ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

  • ํ˜•์‹

    public StringBuffer reverse()

  • ์˜ˆ์ œ

class ReverseExam{
	public static void main(String args[]){
    	StringBuffer sb1 = new StringBuffer("java Programming");
        StringBuffer sb2 = null;
        sb2 = sb1.reverse();
        System.out.println("sb2 = " +sb2);
    }
}

์‹คํ–‰๊ฒฐ๊ณผ

sb2 = gnimmargotP avaj


๋ผ. toString๋ฉ”์†Œ๋“œ

  • String ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

  • ํ˜•์‹

    public String toString()

  • ์˜ˆ์ œ

class ToStringExam{
	public static void main(String args[]){
    StringBuffer sb1 = new StringBuffer("java Programming");
    	String str = null;
        str = sb1.toString();
        System.out.println("str.toUpperCase() = " +str.UpperCase());
    }

}

์‹คํ–‰๊ฒฐ๊ณผ

str.toUpperCase() -> JAVA PROGRAMMING

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ