본문 바로가기
개발&프로그래밍

[JAVA] 자주쓰는 String 클래스 메소드(String Class method)

by 재아군 2022. 3. 20.

자주쓰는 String 클래스의 메소드들에 대해 알아봅니다.

 

startsWith 메소드

문자열이 지정한 문자로 시작하는지 판단한다. 같으면 true, 아니면 false를 리턴한다.

String test ="test";
System.out.println("startsWith : " + test.startsWith("t")); //startsWith : true

 

String 클래스의 startsWith 메소드

public boolean startsWith(String prefix, int toffset) {
    // Note: toffset might be near -1>>>1.
    if (toffset < 0 || toffset > length() - prefix.length()) {
        return false;
    }
    byte ta[] = value;
    byte pa[] = prefix.value;
    int po = 0;
    int pc = pa.length;
    if (coder() == prefix.coder()) {
        int to = isLatin1() ? toffset : toffset << 1;
        while (po < pc) {
            if (ta[to++] != pa[po++]) {
                return false;
            }
        }
    } else {
        if (isLatin1()) {  // && pcoder == UTF16
            return false;
        }
        // coder == UTF16 && pcoder == LATIN1)
        while (po < pc) {
            if (StringUTF16.getChar(ta, toffset++) != (pa[po++] & 0xff)) {
                return false;
           }
        }
    }
    return true;
}


endsWith 메소드

문자열 마지막에 지정한 문자가 있는지 판단한다. 있으면 true, 없으면 false를 리턴한다.

String test ="test";
System.out.println("endsWith : " + test.endsWith("t")); //endsWith : true

 

String 클래스의 endsWith 메소드

 public boolean endsWith(String suffix) {
    return startsWith(suffix, length() - suffix.length());
}


equals 메소드

두개의 String에 값을 비교해서 같으면 true, 다르면 false를 리턴한다.

String test1 ="test";
String test2 ="test";
System.out.println(test1.equals(test2)); // true

 

String 클래스의 equals 메소드

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String aString = (String)anObject;
        if (coder() == aString.coder()) {
            return isLatin1() ? StringLatin1.equals(value, aString.value)
                              : StringUTF16.equals(value, aString.value);
        }
    }
    return false;
}

 


indexOf 메소드

지정한 문자가 문자열에 몇번째에 있는지를 인덱스 위치(int)를 리턴한다.

 

String test ="test";
System.out.println(test.indexOf("e")); //1

 

String 클래스의 indexOf 메소드

 public int indexOf(String str) {
    if (coder() == str.coder()) {
        return isLatin1() ? StringLatin1.indexOf(value, str.value)
                          : StringUTF16.indexOf(value, str.value);
    }
    if (coder() == LATIN1) {  // str.coder == UTF16
        return -1;
    }
    return StringUTF16.indexOfLatin1(value, str.value);
}


lastIndexOf 메소드

문자열에 지정한 문자가 마지막 몇번째에 있는지 인덱스 위치(int)를 리턴한다.

 

String test ="test";
System.out.println(test.lastIndexOf("e")); //1

 

static int lastIndexOf(byte[] src, byte srcCoder, int srcCount,
                           String tgtStr, int fromIndex) {
    byte[] tgt = tgtStr.value;
    byte tgtCoder = tgtStr.coder();
    int tgtCount = tgtStr.length();
    /*
     * Check arguments; return immediately where possible. For
     * consistency, don't check for null str.
     */
    int rightIndex = srcCount - tgtCount;
    if (fromIndex > rightIndex) {
        fromIndex = rightIndex;
    }
    if (fromIndex < 0) {
        return -1;
    }
    /* Empty string always matches. */
    if (tgtCount == 0) {
        return fromIndex;
    }
    if (srcCoder == tgtCoder) {
        return srcCoder == LATIN1
            ? StringLatin1.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex)
            : StringUTF16.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex);
    }
    if (srcCoder == LATIN1) {    // && tgtCoder == UTF16
        return -1;
    }
    // srcCoder == UTF16 && tgtCoder == LATIN1
    return StringUTF16.lastIndexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex);
}


length 메소드

문자열의 길이를 리턴한다.

 

String test ="test";
System.out.println(test.length()); //4

 


replace 메소드

문자열에 지정한 문자가 있으면 새로 지정한 문자로 바꿔서 리턴한다.

 

String test ="test";
System.out.println(test.replace("t","T")); //TesT

 

contains 메소드

두개의 String을 비교해서 해당 String을 포함하고 있으면 true, 다르면 false를 리턴한다.

String test ="test";
System.out.println(test.contains("t")); //true

댓글