Java String split




자바 문자열 split () 메소드는 주어진 정규 표현식에 대해 문자열을 나누고, char 배열을 반환합니다.



Signiture 시그니처


split () 메소드는 2가지 시그니쳐가 있습니다.



Public String split(String regex)

public String split(String regex, int limit)







Parameter 파라메터


regex : 문자열에 사용되는 정규 표현 


limit : 배열에서 문자열 수의 한계. 만약 수가 제로라면, 정규 표현식에 맞는 모든 문자열이 출력됩니다.






Returns

 

문자열의 배열





Throws


PatternSyntaxException : 만약 정규표현식의 패턴이 유효하지 않다면






1. Java String split () method example


주어진 예 - 간격을 제외하고, 스트링 단어의 모든 수를 반환하고, 또한 특수 문자를 포함합니다.


public class SplitExample{
    public static void main(String args[]){
        String[] words = s1.split("\\s");    // 문자열에 기초한 문자열을 나눕니다.
         // 문자열 배열의 엘리먼트를 나타내기 위해 자바 각각의 루프를 사용하는 것

        for(String w : words){
            System.out.println(w);
        }
    }
}
        

java

string

split

method

by

javatpoint






2. Java String split () method with regex and length example

public class SplitExample2{
    public static void main(String args[]){
       String s1="welcome to split world";
       System.out.println("returning words:");

       for(String w:s1.split("\\s",0)){
          System.out.println(w);
       }
       System.out.println("returning words:");

       for(String w:s1.split("\\s",1)){
          System.out.println(w);
       }
       System.out.println("returning words:");

       for(String w:s1.split("\\s",2)){
          System.out.println(w);
       }
    }
}

returning words:

welcome

to

split

world

returning words:

welcome to split world

returning words:

welcome

to split world













Java String startsWith


자바 문자열 startsWith () 메소드는 문자열이 주어진 접두표현을 가지고 시작하는지 확인합니다.

만약 주어진 접두표현을 가지고 시작한다면 true를 반환, 아니라면 false를 반환합니다.





Signiture 시그니처



public boolean startsWith(String prefix)  
public boolean startsWith(String prefix, int offset)  






Parameter 파라미터


prefix: 문자의 순서





Returns

 

true or false





Java String stratsWith () method example



public class StartsWithExample{  
     public static void main(String args[]){  
         String s1="java string split method by javatpoint";  
         System.out.println(s1.startsWith("ja"));  
         System.out.println(s1.startsWith("java string"));  
     }
}  

아웃풋

true

true












Java String substring


자바 문자열 substring () 메소드는 문자열의 한 부분을 반환합니다.

start 인덱스가 포함되고 end 인덱스가 제외된 자바의 부스트링 메소드에 있는, begin 인덱스와 end 인덱스 넘버 포지션를 통과합니다.

다시 말하면, start 인덱스는 0 에서 시작하고, end 인덱스는 1 에서 시작합니다. 

substring 메소드는 2가지 타입이 있습니다.





Signiture 시그니처



public String substring(int startIndex)  

public String substring(int startIndex, int endIndex)  

 

만약 endIndex 를 지정하지 않는 다면, 자바 substring () 메소드는 starIndex 에서 모든 문자들을 반환 할 수 있습니다.







Parameter 파라메터


startIndex : starting index 포함.


endIndex : ending index 제외.





Returns 

 

지정 문자열




Throws


StringIndexOutOfBoundsException 만약 start 인덱스가 부정값이거나 end 인덱스가, 시작하는 인덱스보다 낮다면







Java String substring () method example



public class SubstringExample{  
     public static void main(String args[]){  
          String s1="javatpoint";  
          System.out.println(s1.substring(2,4));//returns va  
          System.out.println(s1.substring(2));//returns vatpoint  
     }
}  

va

vatpoint










영어원문내용출처 :http://www.javatpoint.com/

번역,의역 및 작성 : 초코토끼

검수 : 개발토끼

오역 및 오타의 지적은 겸손히 받겠습니다.



+ Recent posts