2013-04-25 31 views
-1

所需的字符串我有一個像下面[Lcom.hexgen.ro.request.CreateRequisitionRO;爪哇 - 斯普利特,並從字符串

字符串如何從只在java中[Lcom.hexgen.ro.request.CreateRequisitionRO;得到字符串com.hexgen.ro.request.CreateRequisitionRO這一點。有幾種情況下,我得到com.hexgen.ro.request.CreateRequisitionRO;所以我也必須檢查給定的字符串是否具有我期待的格式com.hexgen.ro.request.CreateRequisitionRO

請幫我改正它。

+1

如果它具有可預測的模式,您可以使用REGEX,並請提供您已嘗試的代碼片段 – 2013-04-25 07:36:28

+1

請澄清您的必備條件,因爲很難理解您想要做什麼 – Averroes 2013-04-25 07:37:11

+0

您所看到的是(我相信)數組中包含的「com.hexgen.ro.request.CreateRequisitionRO」類型對象的字符串表示形式。因此檢查數組並獲取第一個元素。 – 2013-04-25 07:38:04

回答

1

下面是一個代碼:

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

如何從[Lcom.hexgen.ro.request.CreateRequisitionRO;

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

這可能工作你的情況,考慮你的字符串替換查詢:

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; 
} 
2

您應該使用正則表達式:

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)); 
} 
0
Use substring function 
String url="com.hexgen.ro.request.CreateRequisitionRO"; 
int n=url.length(); 
String url1=url.substring(
+1

雖然這段代碼可能會回答這個問題,但提供關於_how_和/或_why_的附加上下文可以解決問題,從而提高答案的長期價值。 – 2015-08-10 11:34:59