简体   繁体   中英

Java - Split and get the needed string from the string

i have a string like the following [Lcom.hexgen.ro.request.CreateRequisitionRO;

how to get the string com.hexgen.ro.request.CreateRequisitionRO this only from [Lcom.hexgen.ro.request.CreateRequisitionRO; in java. there are few cases where i get com.hexgen.ro.request.CreateRequisitionRO; so i also have to check whether the given string has the format of what i am expecting like com.hexgen.ro.request.CreateRequisitionRO

Please help me to correct it.

You should be using a regex:

String input = "com.hexgen.ro.request.CreateRequisitionRO"; //OR "[Lcom.hexgen.ro.request.CreateRequisitionRO;"
Pattern p = Pattern.compile("(\\[L)*([\\w\\.]+)(\\;)*");
Matcher m = p.matcher(input);
while(m.find()){
    System.out.println(m.group(2));
}

如何仅从[Lcom.hexgen.ro.request.CreateRequisitionRO;中获取字符串com.hexgen.ro.request.CreateRequisitionRO;

String res = str.substring(2,str.length-1);

Here is a code:

String toBeFixed = "[Lcom.hexgen.ro.request.CreateRequisitionRO;"; 
String toReplaceWith =toBeFixed.replaceAll("\\[L", "").replaceAll("\\;","");
System.out.println(toReplaceWith);

This might work in your case, considering you have string replacement query:

public void test(){
String s1 = "[Lcom.hexgen.ro.request.CreateRequisitionRO";
String s2 = "com.hexgen.ro.request.CreateRequisitionRO";
System.out.println(getClassName(s1));  
System.out.println(getClassName(s2));
}

public String getClassName(String s){
            if(s.startsWith("\\[L")){
                s = s.substring(2, s.length()-1);
            }
      return s;
 }
Use substring function
String url="com.hexgen.ro.request.CreateRequisitionRO";
int n=url.length();
String url1=url.substring(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM