[JAVA 면접문제] 문자열 조작에 대한 상용 함수

33802 단어 JAVA 면접문제.

1. toString() 방법


1) 개념: toString() 메서드는 이 객체 자체(이미 문자열임) 2) 구문을 반환합니다.
public String toString()

3) 매개변수: 없음 4) 반환값: 문자열 자체.5) 코드 인스턴스:
public class Test {
    public static void main(String args[]) {
        String Str = new String("CSDN");

        System.out.print("  :" );
        System.out.println( Str.toString() );
    }
}

결과 출력:
  :CSDN

2. Split() 방법


1) 개념: split () 방법은 주어진 정규 표현식에 따라 문자열을 분할한다.참고:, $, |및 *와 같은 이스케이프 문자는 ===\===을 추가해야 합니다.참고: 여러 개의 구분자는 |를 하이픈으로 사용할 수 있습니다.2) 구문:
public String[] split(String regex, int limit)
3) 매개변수:
  • regex - 정규 표현식 구분자.
  • limit - 분할된 부수입니다.

  • 4) 반환값: 문자열 배열 5) 코드 인스턴스:
    public class Test {
        public static void main(String args[]) {
            String str = new String("Welcome-to-csdn");
     
            System.out.println("-   :" );
            for (String retval: str.split("-")){
                System.out.println(retval);
            }
     
            System.out.println("");
            System.out.println("-   :" );
            for (String retval: str.split("-", 2)){
                System.out.println(retval);
            }
     
            System.out.println("");
            String str2 = new String("www.csdn.com");
            System.out.println("  :" );
            for (String retval: str2.split("\\.", 3)){
                System.out.println(retval);
            }
     
            System.out.println("");
            String str3 = new String("acount=? and uu =? or n=?");
            System.out.println("  :" );
            for (String retval: str3.split("and|or")){
                System.out.println(retval);
            }
        }
    }
    

    실행 결과
    -   :
    Welcome
    to
    csdn
    
    -   :
    Welcome
    to-csdn
    
      :
    www
    csdn
    com
    
      :
    acount=? 
     uu =? 
     n=?
    

    3. charAt() 방법


    1) 개념: charAt() 메서드는 지정된 색인에 있는 문자를 반환하는 데 사용됩니다.인덱스 범위는 0부터 length()-1까지입니다.2) 구문:
    public char charAt(int index)
    

    3) 매개 변수: index - 문자의 인덱스 4) 반환값: 지정된 인덱스에 있는 문자를 반환합니다.5) 코드 인스턴스:
    public class Test {
        public static void main(String args[]) {
            String s = "www.csdn.com";
            char result = s.charAt(6);
            System.out.println(result);
        }
    }
    

    출력 결과: d

    4、replace() 방법


    1) 개념: replace () 방법은 newChar 문자로 문자열에 나타난 모든oldChar 문자를 대체하고 교체된 새 문자열을 되돌려줍니다.2) 구문:
    public String replace(char oldChar,
                          char newChar)
    

    3) 매개변수:
  • oldChar – 원래 문자
  • newChar – 새 문자
  • 4) 반환값:바꿀 때 생성된 새 문자열 5) 인스턴스:
    public class Test {
        public static void main(String args[]) {
            String Str = new String("hello");
    
            System.out.print("  :" );
            System.out.println(Str.replace('o', 'T'));
    
            System.out.print("  :" );
            System.out.println(Str.replace('l', 'D'));
        }
    }
    

    결과 출력:
      :hellT
      :heDDo
    

    5. replaceAll() 방법


    1) 개념: replaceAll() 방법은 주어진 매개 변수 replacement를 사용하여 주어진 정규 표현식과 일치하는 모든 하위 문자열을 대체합니다.2) 구문:
    public String replaceAll(String regex, String replacement)
    

    3) 매개변수:
  • regex - 이 문자열과 일치하는 정규 표현식
  • newChar - 일치하는 항목마다 문자열 바꾸기
  • 4) 값이 반환됨: 대체된 문자열이 성공적으로 반환되고 실패할 경우 원래 문자열이 반환됩니다.5) 인스턴스:
    public class Test {
        public static void main(String args[]) {
            String Str = new String("www.google.com");
    
            System.out.print("  :" );
            System.out.println(Str.replaceAll("(.*)google(.*)", "runoob" ));
            System.out.print("  :" );
            System.out.println(Str.replaceAll("(.*)taobao(.*)", "runoob" ));
        }
    }
    

    결과 출력:
      :runoob
      :www.google.com
    

    6、concat () 방법


    1) 개념: concat () 방법은 지정한 문자열 파라미터를 문자열에 연결하는 데 사용됩니다.2) 구문:
    public String concat(String s)
    

    3) 매개변수:
  • s - 연결할 문자열입니다.

  • 4) 반환값: 연결된 새 문자열을 반환합니다.5) 코드 인스턴스:
    public class Test {
        public static void main(String args[]) {
            String s = "CSDN:";
            s = s.concat("www.csdn.com");
            System.out.println(s);
        }
    }
    

    결과 출력:
    CSDN:www.csdn.com
    

    요약: 1.concat의 계산 효율은 +의 효율보다 높다.concat은string과string의 결합에만 적용되며 +string과 그 어떠한 대상의 결합에도 적용됩니다.소량의 데이터 연결을 사용할 때 concat과 +를 사용해도 됩니다. 만약 대량의 데이터 연결을 사용한다면 StringBuilder나StringBuffer를 사용하는 것을 권장합니다.

    7.substring() 방법


    1) 개념:substring () 방법은 문자열의 하위 문자열을 되돌려줍니다.2) 구문:
    public String substring(int beginIndex)
    //  
    public String substring(int beginIndex, int endIndex)
    

    3) 매개변수:
  • beginIndex – 시작 인덱스(포함), 인덱스는 0부터
  • endIndex – 끝 인덱스(포함하지 않음)
  • 4) 반환값: 하위 문자열 5) 코드 인스턴스:
    public class Test {
        public static void main(String args[]) {
            String Str = new String("www.runoob.com");
     
            System.out.print("  :" );
            System.out.println(Str.substring(4) );
     
            System.out.print("  :" );
            System.out.println(Str.substring(4, 10) );
        }
    }
    

    결과 출력:
      :runoob.com
      :runoob
    

    8.trim() 방법


    1) 개념: trim () 방법은 문자열의 머리와 꼬리의 공백을 삭제하는 데 사용됩니다.2) 구문:
    public String trim()
    

    3) 매개변수:
  • 없음
  • 4) 반환값: 머리와 꼬리의 공백을 제거하는 문자열 5) 코드 인스턴스:
    public class Test {
        public static void main(String args[]) {
            String Str = new String("    www.runoob.com    ");
            System.out.print("  :" );
            System.out.println( Str );
    
            System.out.print("  :" );
            System.out.println( Str.trim() );
        }
    }
    

    결과 출력:
      :    www.runoob.com    
      :www.runoob.com
    

    9. toUpperCase() 방법


    1) 개념: toUpperCase() 메서드는 문자열 소문자를 대문자로 변환합니다.2) 구문:
    public String toUpperCase()
     
    public String toUpperCase(Locale locale)
    

    3) 매개변수:
  • 없음
  • 4) 반환값: 문자가 대문자로 변환된 문자열 5) 코드 인스턴스:
    public class Test {
        public static void main(String args[]) {
            String Str = new String("www.runoob.com");
    
            System.out.print("  :" );
            System.out.println( Str.toUpperCase() );
        }
    }
    

    결과 출력:
      :WWW.RUNOOB.COM
    

    10. toLowerCase() 방법


    1) 개념: toLowerCase() 메서드는 문자열을 소문자로 변환합니다.2) 구문:
    public String toLowerCase()
     
    public String toLowerCase(Locale locale)
    

    3) 매개변수:
  • 없음
  • 4) 반환값: 소문자로 변환된 문자열.5) 코드 인스턴스:
    public class Test {
        public static void main(String args[]) {
            String Str = new String("WWW.RUNOOB.COM");
    
            System.out.print("  :" );
            System.out.println( Str.toLowerCase() );
        }
    }
    

    결과 출력:
      :www.runoob.com
    

    10. index () 방법


    1) 개념: index () 함수는 목록에서 어떤 값의 첫 번째 일치하는 색인 위치를 찾아내는 데 사용되며, 위치는 아래 표시 0에서 시작합니다!2) 구문:
    l=[“an”,“Aynor”,1,3]
    l.index(1)
    

    3) 매개변수:
  • 포지셔닝할 수치의 아래 첨자
  • 4) 반환 값: 아래 첨자에 해당하는 데이터 5) 코드 인스턴스:
    l=[“an”,“Aynor”,1,3]
    System.out.print(index(1));
    

    결과 출력:
    Aynor
    

    좋은 웹페이지 즐겨찾기