본문 바로가기
개발/java

String(문자열) 가공하기

by 가시죠 2021. 1. 17.
반응형
package com.tistory.hitomis;

public class Main {

    public static void main(String[] args) {
        String text = "Hello String Test";
        String text2 = "hello String Test";

        display("text 내용 : "+text);
        display("text2 내용 : "+text2);
        display("text.charAt(2) : "+text.charAt(2));
        display("text.concat(\" ...\") : "+text.concat(" ..."));
        display("text.contains(\"String\") : "+text.contains("String"));
        display("text.equals(text2) : "+text.equals(text2));
        display("text.equalsIgnoreCase(text2) : "+text.equalsIgnoreCase(text2));
        display("text.indexOf(\"S\") : "+text.indexOf("S"));
        display("text.lastIndexOf(\"S\") : "+text.lastIndexOf("S"));
        display("text.trim() : "+text.trim());
        display("text.length() : "+text.length());
        display("text.substring(6) : "+text.substring(6));
        display("text.substring(6,10) : "+text.substring(6,10));
        display("text.replace(\" \",\"_\") : "+text.replace(" ","_"));
        display("text.replaceAll(\" \",\"_\") : "+text.replaceAll(" ","_"));
        display("text.toUpperCase() : "+text.toUpperCase());
        display("text.toLowerCase() : "+text.toLowerCase());

        display("for i");
        String[] sp=text.split(" ");
        for (int i = 0; i < sp.length; i++) {
            display(i+"\t"+sp[i]+"\t"+sp[i].length());
        }
        display("for each");
        int j=0;
        for (String s: sp) {
            display(j+"\t"+s+"\t"+s.length());
            j++;
        }
    }

    public static void display(String s) {
        System.out.println(s);
    }
    public static void display(int i) {
        System.out.println(i);
    }
    public static void display(boolean b) {
        System.out.println(b);
    }
}

실행결과

text 내용 : Hello String Test
text2 내용 : hello String Test
text.charAt(2) : l
text.concat(" ...") : Hello String Test ...
text.contains("String") : true
text.equals(text2) : false
text.equalsIgnoreCase(text2) : true
text.indexOf("S") : 6
text.lastIndexOf("S") : 6
text.trim() : Hello String Test
text.length() : 17
text.substring(6) : String Test
text.substring(6,10) : Stri
text.replace(" ","_") : Hello_String_Test
text.replaceAll(" ","_") : Hello_String_Test
text.toUpperCase() : HELLO STRING TEST
text.toLowerCase() : hello string test
for i
0   Hello    5
1   String   6
2   Test     4
for each
0   Hello    5
1   String   6
2   Test     4

Process finished with exit code 0
​
반응형

댓글