고양이 여름이의 지식채널

[Java] 문자열 class, method 예제 본문

Programming/JAVA

[Java] 문자열 class, method 예제

썸머캣 2023. 2. 6. 00:06

Java에서는 java.lang.String 클래스는 문자열 작업에 가장 일반적으로 사용되는 클래스 중 하나입니다. 문자열은 일련의 문자이며 일반적으로 텍스트 데이터를 나타내는데 사용됩니다.

다음은 String 클래스의 주요 메서드와 사용 방법입니다.

 


String 클래스 Method

public class Main {
  public static void main(String[] args) {
    String name = "Lionel Messi";
    
    int length = name.length();
    System.out.println("The length of the name is: " + length);
    
    char firstCharacter = name.charAt(0);
    System.out.println("The first character of the name is: " + firstCharacter);
    
    boolean isEqual = name.equals("Lee Messi");
    System.out.println("The name is equal to 'Lionel Messi': " + isEqual);
    
    boolean startsWithL = name.startsWith("L");
    System.out.println("The name starts with 'L': " + startsWithL);
    
    String replacedName = name.replace("Lionel", "Lee");
    System.out.println("The replaced name is: " + replacedName);
    
    String sub = name.substring(7, 12);
    System.out.println("The substring of the name is: " + sub);
    
    String[] subStrings = name.split(" ");
    System.out.println("The first part of the name is: " + subStrings[0]);
    System.out.println("The second part of the name is: " + subStrings[1]);
  }
}
  • length() - 문자열의 길이를 반환합니다

    charAt(int index) - 지정한 인덱스의 문자를 반환합니다.

    equals(Object obj) -  두 문자열을 비교합니다 (반환값 - 참,거짓)

    equalsIgnoreCase(String anotherString) - 두 문자열을 비교하고 대소문자를 무시합니다.

    startsWith(String 접두사) - 문자열이 지정된 접두사로 시작하는지 여부를 결정합니다.

    endsWith(String 접미사) - 문자열이 지정된 접미사로 끝나는지 여부를 결정합니다.

    contains(CharSequences) - 문자열에 지정된 문자가 포함되어 있는지 여부를 결정합니다.

    replace(charoldChar, charnewChar) - 지정한 문자의 모든 항목을 새 문자로 바꿉니다.

    subString(int beginIndex, int endIndex) - index를 기준으로 문자열을 자릅니다.

    split(String regex) - 지정된 정규식, 문자열 등 을 사용하여 문자열을 배열로 분할합니다.

 

반응형

 

output

The length of the name is: 12

The first character of the name is: L

The name is equal to 'Lee Messi': false

The name starts with 'L': true

The replaced name is: Lee Messi

The substring of the name is: Messi

The first part of the name is: Lional

The second part of the name is: Messi

[Java] Java Collections. (컬렉션)

 

[Java] Java Collections. (컬렉션)

Java Collection. Java Collection 프레임워크는 컬렉션을 저장하고 조작하기 위한 통합 아키텍처를 제공하는 클래스와 인터페이스의 그룹입니다. Java는 Collection 프레임워크를 구현하는 ArrayList, LinkedList,

summer-cat93.tistory.com

 

 

728x90
반응형
Comments