2015-04-14 51 views
0

它基本上是關於獲取兩個字符之間的字符串值。 SO有很多與此有關的問題。像:Java String-如何在android中獲取包名稱的一部分?

How to get a part of a string in java?

How to get a string between two characters?

Extract string between two strings in java

多。 但我覺得它在處理字符串中的多個點並獲得特定兩個點之間的值時很困惑。

我已經拿到了包名稱:

au.com.newline.myact 

我需要得到的值「融爲一體」。和下一個「點(。)」。在這種情況下,「換行」。我試過

Pattern pattern = Pattern.compile("com.(.*)."); 
Matcher matcher = pattern.matcher(beforeTask); 
while (matcher.find()) { 
     int ct = matcher.group(); 

我試過使用子字符串和IndexOf也。但無法得到預期的答案。由於android中的包名稱因點數和字符數的不同而不同,因此我無法使用固定索引。請提出任何想法。

回答

2

正如你可能知道返回數組了(根據你的正則表達式.*部分)dot.是在正則表達式特殊字符表示任何字符(行分隔符除外)。所以要真正讓點代表只有點你需要逃避它。要做到這一點,您可以在其之前放置\,或將其放置在character class[.]之內。

此外,要從括號(.*)只得到一部分,你需要選擇適當的group索引,在你的情況是1

因此,與嘗試

String beforeTask = "au.com.newline.myact"; 
Pattern pattern = Pattern.compile("com[.](.*)[.]"); 
Matcher matcher = pattern.matcher(beforeTask); 
while (matcher.find()) { 
    String ct = matcher.group(1);//remember that regex finds Strings, not int 
    System.out.println(ct); 
} 

輸出:newline


如果你想下一個.那麼你就需要通過增加.**量詞的改變greedy behaviourreluctant之前得到的只有一個元素?之後它就像

Pattern pattern = Pattern.compile("com[.](.*?)[.]"); 
//          ^

另一種方法是,而不是.*只接受非點字符。他們可以通過negated character class表示:[^.]*

Pattern pattern = Pattern.compile("com[.]([^.]*)[.]"); 

如果你不想使用正則表達式,你可以簡單地使用indexOf方法後,它來定位的com.和明年.位置。然後你可以簡單地將你想要的東西串起來。

String beforeTask = "au.com.newline.myact.modelact"; 
int start = beforeTask.indexOf("com.") + 4; // +4 since we also want to skip 'com.' part 
int end = beforeTask.indexOf(".", start); //find next `.` after start index 
String resutl = beforeTask.substring(start, end); 
System.out.println(resutl); 
+0

這是對我錯誤的出色修正。我錯過了[]中包裝點的部分。但是,如果包名稱包含更多點,例如:au.com.newline.myact.modelact「,則此代碼將返回」newline.myact「的值。 – Shasi

+0

@Shasi,您希望獲得什麼值? 'newline'? – Pshemo

+0

是的,我只需要換行符 – Shasi

2

您所要做的就是將字符串拆分爲「。」。然後遍歷它們直到找到等於「com」的一個。數組中的下一個字符串將是你想要的。

所以,你的代碼看起來是這樣的:

String[] parts = packageName.split("\\."); 
int i = 0; 
for(String part : parts) { 
    if(part.equals("com") 
     break; 
    } 
    ++i; 
} 
String result = parts[i+1]; 
+0

你測試你的'分裂( 「」)'? – Pshemo

+0

好主意。只是一個更正,因爲我已經增加了,最後一行是 String result = parts [i]; – Shasi

2

您可以使用反射來得到任何類的名稱。例如:

如果我有一個類Runnercom.some.package,我可以運行

Runner.class.toString() // string is "com.some.package.Runner" 

得到恰好有內包名稱類的全名。

要獲得「COM」之後的東西,你可以使用Runner.class.toString().split("."),然後重複與布爾標誌

+0

你確定關於'split(「,」)'部分? – Pshemo

+0

你也確定'split(「。」)'部分? – Pshemo

+0

接下來的一點是更好的:)(這是讓人們閱讀'split'文檔的第一個錯誤之一,正如我在這裏看到所有的答覆者都犯了這個錯誤) – Pshemo

1
private String getStringAfterComDot(String packageName) { 
     String strArr[] = packageName.split("\\."); 
     for(int i=0; i<strArr.length; i++){ 
      if(strArr[i].equals("com")) 
       return strArr[i+1]; 
     } 
     return ""; 
    } 
+0

你測試了split(「。」)的結果嗎? – Pshemo

+0

@Pshemo對不起,我沒有在IDE中運行它。指的是,謝謝。 –

1

我曾經做過的項目堆處理網站抄襲之前,我 只需要創建我自己的函數/ utils的把工作完成。正則表達式可能 有時候是一種矯枉過正,如果你只是想從 提取一個給定的字符串像你有一個子字符串。以下是我通常使用 來完成這類任務的功能。

private String GetValueFromText(String sText, String sBefore, String sAfter) 
{  
    String sRetValue = ""; 
    int nPos = sText.indexOf(sBefore); 
    if (nPos > -1) 
    { 
    int nLast = sText.indexOf(sAfter,nPos+sBefore.length()+1); 
    if (nLast > -1) 
    { 
     sRetValue = sText.substring(nPos+sBefore.length(),nLast); 
    } 
    } 
    return sRetValue; 
} 

要使用它,請執行下列操作:

String sValue = GetValueFromText("au.com.newline.myact", ".com.", "."); 
相關問題