Splitting a String in Java with underscore as delimiter

后端 未结 7 1016
野的像风
野的像风 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:12

    You say

    This format for the string is generic.

    Then concatenate the elements with indexes 2 and 3 after splitting:

    String str = "abc_2012-10-18-05-37-23_prasad_hv_Complete";
    String[] parts = str.split("_");
    String extractedResult = "";
    if(parts.length > 3)
       extractedResult = parts[2] + "_" + parts[3]; // prasad_hv is captured here.
    

提交回复
热议问题