간단한 java 연습은 아래 링크를 통해, 웹상에서 진행해볼 수 있습니다.

https://www.tutorialspoint.com/compile_java_online.php

 

 

 

1. indexOf

문자열 첫 출현 위치 출력

H e l  l o    J a v a

0 1 2 3 4 5 6 7 8 9

public class Growing_Test{
     public static void main(String []args){
        String a = "Hello Java";
        System.out.println(a.indexOf("Java"));
     }
}

 

 

 

2. replaceAll

특정 문자열 다른 문자열로 바꾸기

public class Growing_Test{
     public static void main(String []args){
        String a = "Hello Java";
        System.out.println(a.replaceAll("Java", "World"));
     }
}

 

 

 

3. substring

a.substring(0, 3) -> 0, 1, 2 번째 문자 출력

a.substring(2, 3) -> 2 번째 문자 출력

시작위치 <= a < 마지막위치

public class Growing_Test{
     public static void main(String []args){
        String a = "Hello Java";
        System.out.println(a.substring(0, 3));
        System.out.println(a.substring(2, 3));
     }
}

 

 

 

4. toUpperCase

모든 문자 대문자화하기

public class Growing_Test{
     public static void main(String []args){
        String a = "Hello Java";
        System.out.println(a.toUpperCase());
     }
}

 

 

 

5. StringBuffer : append

StringBuffer로 객체 생성시, 일반 String에 비해 메모리 사용량이 많고 속도가 느리므로 문자열 추가나 변경 등의 작업이 많을 경우 StringBuffer 사용을 권장합니다.

 - StrinfBuffer 활용

public class Growing_Test{
     public static void main(String []args){
        StringBuffer sb = new StringBuffer();
        sb.append("hello");
        sb.append(", ");
        sb.append("jump to java");
        System.out.println(sb.toString());
     }
}

 - StrinfBuffer 미활용

public class Growing_Test{
    public static void main(String[] args) {
        String temp = "";
        temp += "hello";
        temp += ", ";
        temp += "jump to java";
        System.out.println(temp);
    }
}

StrinfBuffer 활용시 객체는 sb 한번만 생성되지만, 미활용시 + 연산이 있을 때마다 새로운 String 객체가 생성되기때문에 총 4개의 String 자료형 객체가 만들어집니다.

 

 

 

6. StringBuffer : insert

sb의 6 위치에 "jump" 문구를 삽입합니다.

public class Growing_Test{
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("hello to java");
        sb.insert(6, "jump ");
        System.out.println(sb.toString());
    }
}

 

 

 

7. StringBuffer : substring

6,7,8,9 위치의 문자를 출력합니다.

public class Growing_Test{
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("Hello jump to java");
        System.out.println(sb.substring(6, 10));
    }
}

 

 

 

 

 

+ Recent posts