๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java-๊ธฐ์ดˆ] ๊ทธ ๋ฐ–์˜ ๋‹ค๋ฅธ String ๊ด€๋ จ ๋ฉ”์„œ๋“œ

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 21.
๋ฐ˜์‘ํ˜•

โ–ถ ๊ทธ ๋ฐ–์˜ ๋‹ค๋ฅธ String ๊ด€๋ จ ๋ฉ”์„œ๋“œ

package string;

public class StringMethod {
	public static void main(String[] args) {
		int alength, blength;
		char achar, bchar;
		String mystr1 = new String("Java Powerful");
		String mystr2 = new String("Java Programming");
		String mystr3 = mystr1 + mystr2;
		String mystr4 = "java powerful";

		alength = mystr1.length();
		blength = mystr2.length();
		achar = mystr1.charAt(5);
		bchar = mystr2.charAt(10);

		System.out.println("mystr1์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” " + mystr1 + "์˜ ๋ฌธ์ž์˜ ๊ธธ์ด๋Š” " + alength);
		System.out.println("mystr2์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” " + mystr2 + "์˜ ๋ฌธ์ž์˜ ๊ธธ์ด๋Š” " + blength);
		System.out.println("mystr1์˜ 5๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๋ฌธ์ž๋Š” " + achar);
		System.out.println("mystr2์˜ 10๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๋ฌธ์ž๋Š” " + bchar);
		System.out.println("mystr1์˜ ์ €์žฅ๋œ ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟˆ : " + mystr1.toUpperCase()); // ํ•œ๊ธ€์€ ์•ˆ๋จ.
		System.out.println("mystr2์˜ ์ €์žฅ๋œ ๋ฌธ์ž ์ค‘ a๋ฅผ A๋กœ ๋ฐ”๊ฟˆ : " + mystr2.replace('a', 'A')); // ๋ฌธ์ž์—ด๋„ ๊ฐ€๋Šฅ
		System.out.println("mystr1์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด๊ณผ mystr4์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ฐ’์„ ๋น„๊ต(๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„) : " + mystr1.equals(mystr4));
		System.out.println("mystr1์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด๊ณผ mystr4์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ฐ’์„ ๋น„๊ต(๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์•ˆํ•จ) : " + mystr1.equalsIgnoreCase(mystr4));
		System.out.println("concat() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ mystr1์— mystr2 ๋ถ™์ด๊ธฐ : " + mystr1.concat(mystr2));
		System.out.println("mystr1์˜ ์ฒซ๊ธ€์ž๋ถ€ํ„ฐ 4๊ธ€์ž ์ถœ๋ ฅ : " + mystr1.substring(0, 3));
		System.out.println("mystr1์˜ 6๋ฒˆ์งธ ๊ธ€์ž๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ถœ๋ ฅ (powerful) : " + mystr1.substring(5));
		System.out.println("mystr1์— ์ ค ๋จผ์ € ๋‚˜์˜ค๋Š” 'a'์˜ ์œ„์น˜ : " + mystr1.indexOf('a')); // ๊ธ€์ž ์œ„์น˜๊ฐ€ ์—†์œผ๋ฉด -1 ๊ฐ’์„ ๋ฆฌํ„ดํ•จ.
		System.out.println("mystr1์— ์ ค ๋‚˜์ค‘์— ๋‚˜์˜ค๋Š” 'a'์˜ ์œ„์น˜ : " + mystr1.lastIndexOf('a'));
		System.out.println("mystr1์˜ 5๋ฒˆ ๋ฌธ์ž๋ถ€ํ„ฐ ๋๊นŒ์ง€์˜ ๊ธธ์ด : " + mystr1.substring(4).length());
		System.out.println("mystr1์˜ 5๋ฒˆ ๋ฌธ์ž๋ถ€ํ„ฐ ๋๊นŒ์ง€์˜ ๊ธธ์ด trim() ์‚ฌ์šฉ : " + mystr1.substring(4).trim().length());
	}
}

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

 

 

 

*String๊ณ„์—ด - split( ) ๋ฉ”์„œ๋“œ

split์€ ๋ฌธ์ž์—ด์—์„œ ๋‚˜๋ˆ„๊ณ  ์‹ถ์€ ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ ์„œ ๋ฐฐ์—ด๋กœ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

์ฆ‰, regex๋ฅผ ๋„˜๊ฒจ์ค˜์„œ ํ•ด๋‹น regex๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ถ„๋ฆฌํ•˜์—ฌ String[ ] ํƒ€์ž…์œผ๋กœ return ํ•ด์ค€๋‹ค.

split( ) ์˜ ๊ด„ํ˜ธ( ) ์•ˆ์ชฝ์—๋Š” ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ํ•œ์ •๋˜์–ด ์žˆ๋‹ค. ex) @@, " ", \\ ๋“ฑ

JDK 1.4๋ถ€ํ„ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.

โ€‹

โ€‹

StringMethod2.java

package string;

public class StringMethod2 {
	public static void main(String[] args) {
		String str1 = "java programming is powerful";
		String arr1[] = str1.split(" ");  // " " ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ
		System.out.println(str1);
		System.out.println("split ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ : ");
		for (int i = 0; i < arr1.length; i++) {
			System.out.println("๋ฐฐ์—ด[" + i + "] ๋ฒˆ์งธ ๊ฐ’ : " + arr1[i]);
		}
		System.out.println();

		String str2 = "2020-06-17";
		String arr2[] = str2.split("-");  // "-" ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ
		System.out.println(str2);
		for (int i = 0; i < arr2.length; i++) {
			System.out.println(arr2[i] + "\t");
		}
	}
}

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

โ€‹

 

 

*String๊ณ„์—ด - StringTokenizer ํด๋ž˜์Šค

๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค

์ด๋ ‡๊ฒŒ ๋ถ„๋ฆฌ๋œ ๊ฐ’์„ ํ† ํฐ(Token)์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ด๋Ÿฐ ๊ธฐ๋Šฅ์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š”๊ฒƒ์ด StringTokenizer ํด๋ž˜์Šค๋‹ค.

์ƒ์„ฑ์ž

๋ฉ”์„œ๋“œ๋ช…

์„ค๋ช…

int

countTokens()

token๋œ ์ž์›์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

boolean

hasMoreTokens()

tokenํ•  ์ˆ˜ ์žˆ๋Š” ์ž์›์ด ์žˆ์„ ๊ฒฝ์šฐ์—” true, ์—†์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

String

nextToken()

token๋œ ์ž์›์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

โ€‹

-๊ณต๋ฐฑ์œผ๋กœ ์ถœ๋ ฅ

package string;

import java.util.StringTokenizer;

public class StringTokenProg {
	public static void main(String[] args) {
		StringTokenizer str = new StringTokenizer("2008/02/15", "/");
		int count;

		count = str.countTokens();
		System.out.println("ํ”ผ์‹ฑํ•  ๋ฌธ์ž์—ด์˜ ์ˆ˜๋Š” ์ด : " + count + "๊ฐœ");

		while (str.hasMoreTokens()) {  // return ๊ฐ’ : ๋‹ค์Œ ํ† ํฐ์ด ์žˆ์œผ๋ฉด true, ์—†์œผ๋ฉด false
			System.out.println(str.nextToken);  // ํ˜„์žฌ ์œ„์น˜์—์„œ ๊ทธ ๋‹ค์Œ ํ† ํฐ์„ returnํ•จ
		}

//      String[] str1;
//      str1 = new String[count];
//      for (int i = 0; i < count; i++) {
//      str1[i] = str.nextToken();
//      system.out.println(str1[i]);
	}
}

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

โ€‹

 

- /๋กœ ์ถœ๋ ฅ

package string;

import java.util.StringTokenizer;

public class StringTokenProg {
	public static void main(String[] args) {
		StringTokenizer str = new StringTokenizer("2008/02/15", "/");
		int count;

		count = str.countTokens();
		System.out.println("ํ”ผ์‹ฑํ•  ๋ฌธ์ž์—ด์˜ ์ˆ˜๋Š” ์ด : " + count + "๊ฐœ");

		while (str.hasMoreTokens()) { // return ๊ฐ’ : ๋‹ค์Œ ํ† ํฐ์ด ์žˆ์œผ๋ฉด true, ์—†์œผ๋ฉด false
			String strToken = str.nextToken();
			System.out.println(strToken);
			if (strToken.equals("2008")) {
				strToken = str.nextToken("2");
				System.out.println(strToken);
			}
		}
	}
}

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

โ€‹

โ˜… split( ) ๋ฉ”์„œ๋“œ์™€ StringTokenizer ํด๋ž˜์Šค์˜ ์ฐจ์ด

split( ) ๋ฉ”์„œ๋“œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ์ˆ˜๋งŒํผ์˜ ๋ถ„ํ• ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ณต๋ฐฑ๋„ ๋ฌด์‹œํ•˜์ง€์•Š์Œ.

๊ทธ๋Ÿฌ๋‚˜ StringTokenizer ํด๋ž˜์Šค๋Š” ๊ตฌ๋ถ„๋ฌธ์ž๋กœ ๋ถ„ํ• ๋˜๋Š” ์œ„์น˜์— ํ•„์š”์—†๋Š” ๋ถ„ํ• ๊ณต๊ฐ„์ด ์žˆ์„ ๊ฒฝ์šฐ์—” ์ œ๊ฑฐ๋œ๋‹ค.

โ€‹

โ€‹โ€‹

โ€‹

์‹ค์Šต) ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๋ฉด ๊ฑฐ๊พธ๋กœ ์ถœ๋ ฅ๋˜๊ฒŒ ๋งŒ๋“ค์–ด๋ณด๋ผ.

์กฐ๊ฑด) reverse( ) ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ.

์ด๋ ‡๊ฒŒ ๋‚˜์˜ค๊ฒŒ!!!

 

<๋‚ด๊ฐ€ ํ•œ ๊ฒƒ>

package string;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StringReverse {
	public static void main(String[] args) throws IOException {
		BufferedReader inbr = new BufferedReader(new InputStreamReader(System.in));
		String a;

		System.out.print("๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		a = inbr.readLine();  // readLine() ์ž์ฒด๊ฐ€ String ์œผ๋กœ ๋ฐ˜ํ™˜.

		StringBuffer str = new StringBuffer(a);
		str.reverse();  // ๋ฌธ์ž์—ด ๊ฑฐ๊พธ๋กœ ์ถœ๋ ฅ

		System.out.println("์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด : " + a);

		System.out.println("๊ฑฐ๊พธ๋กœ ๋ฐ”๋€ ๋ฌธ์ž์—ด : " + str.toString());
	}
}

 

๋”๋ณด๊ธฐ

<์„ ์ƒ๋‹˜์ด ๋งŒ๋“  ๊ฒƒ>

package string;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StringReverse_A {
	public static void main(String[] args) throws IOException {
		System.out.print("๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader inbr = new BufferedReader(isr);
		String instr = inbr.readLine();
		StringReverse_A sr = new StringReverse_A();

		System.out.println("์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด : " + instr);
		System.out.print("๊ฑฐ๊พธ๋กœ ๋ฐ”๋€ ๋ฌธ์ž์—ด : " + sr.reverse(instr));
	}

	public String reverse(String instr) {
		String outStr = "";
		for (int i = instr.length() - 1; i >= 0; i--) {
			outStr += instr.charAt(i);
		}
		return outStr;
	}
}

 

<์„ ์ƒ๋‹˜์ด ๋งŒ๋“ ๊ฒƒ 2>

package string;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StringReverse_A {
	public static void main(String[] args) throws IOException {
		System.out.print("๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader inbr = new BufferedReader(isr);
		String instr = inbr.readLine();
		StringReverse_A sr = new StringReverse_A();

		System.out.println("์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด : " + instr);
		System.out.print("๊ฑฐ๊พธ๋กœ ๋ฐ”๋€ ๋ฌธ์ž์—ด : " + sr.reverse(instr));
	}

	public String reverse(String instr) {  // reverse ๋ฉ”์„œ๋“œ ์ง์ ‘ ์„ ์–ธ
		String outStr = "";
		for (int i = instr.length() - 1; i >= 0; i--) {
			outStr += instr.charAt(i);
		}
		return outStr;
	}

	public String reverse1(String instr) {  // StringBuffer์„ ์‚ฌ์šฉํ•ด reverse ๋ฉ”์„œ๋“œ ์„ ์–ธ
		StringBuffer sb = new StringBuffer(instr);
		return sb.reverse().toString();
	}
}

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€