Splitting a String in Java with underscore as delimiter

后端 未结 7 1071
野的像风
野的像风 2021-01-13 00:49

I have the following String (This format for the string is generic)

abc_2012-10-18-05-37-23_prasad_hv_Complete

I want to extract only

7条回答
  •  难免孤独
    2021-01-13 01:23

    What I understand by your question is that you want 3rd element to (n-1)th element of the String split by "_" to come as it is (where n is the length of array formed after splitting). So, the code would be like this:

    import java.util.Arrays;
    
    public class StringSplit {
        public static final String test = "abc_2012-10-18-05-37-23_prasad_hv_Complete";
        public static void main(String args[]){
            String[] data = test.split("_");
            System.out.println(Arrays.toString(data));
            String aim = data[2];
            for(int i=3;i

提交回复
热议问题