简体   繁体   English

Java-拆分并从字符串中获取所需的字符串

[英]Java - Split and get the needed string from the string

i have a string like the following [Lcom.hexgen.ro.request.CreateRequisitionRO; 我有一个类似下面的字符串[Lcom.hexgen.ro.request.CreateRequisitionRO;

how to get the string com.hexgen.ro.request.CreateRequisitionRO this only from [Lcom.hexgen.ro.request.CreateRequisitionRO; 如何获得字符串com.hexgen.ro.request.CreateRequisitionRO这只能从[Lcom.hexgen.ro.request.CreateRequisitionRO; in java. 在Java中。 there are few cases where i get com.hexgen.ro.request.CreateRequisitionRO; 在少数情况下,我会收到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 所以我还必须检查给定的字符串是否具有我期望的格式,例如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(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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