2013-07-25 77 views
0

我試圖從String獲取int。該String會經常來的:從大字符串中提取int

"mombojumbomombojumbomombojumbomombojumbomombojumbomombojumbohello=1?fdjaslkd;fdsjaflkdjfdklsa;fjdklsa;djsfklsa;dfjklds;afj=124214fdsamf=352" 

在所有這一切的唯一不變的是,我將有一個"hello="後跟一個數字。正因如此,我無法弄清楚如何在"hello="之後拉出數字。這是我迄今爲止嘗試過的,沒有運氣。

編輯:數字後面總會跟着一個「?」

String[] tokens = s.split("hello="); 
for (String t : tokens) 
    System.out.println(t); 

我想不出如何從int的兩側隔離它。

+1

你有多大的字符串粗略,並且有多重要是表現?我問,除非你的字符串非常大,性能非常關鍵,那麼正則表達式可以很容易地處理。 –

+0

就是這個尺寸。不會更大。 – EGHDK

+0

改爲使用regex。非常類似於這個線程http://stackoverflow.com/questions/2127898/use-regex-to-extract-integer-from-url-in-a-string-with-php – VadimM

回答

13
Pattern p = Pattern.compile("hello=(\\d+)"); 
Matcher m = p.matcher (s); 
while (m.find()) 
    System.out.println(m.group(1)); 

這建立用於任何地方s包含你好搜索=後面跟着一個或多個數字(\\d+指一個或多個數字)。循環查找此模式的每次出現,然後在找到匹配時,m.group(1)提取數字(因爲這些數字按模式分組)。

+1

小心解釋你的代碼? –

+1

我添加了一個解釋。希望它有幫助。 – ajb

+0

這項工作是「你好= 27」嗎?它會返回27嗎? – EGHDK

1

試試這個:

String r = null; 
int col = s.indexOf("hello="); // find the starting column of the marker string 
if (col >= 0) { 
    String s2 = s.substring(col + 6); // get digits and the rest (add length of marker) 
    col = 0; 
    // now find the end of the digits (assume no plus or comma or dot chars) 
    while (col < s2.length() && Character.isDigit(s2.charAt(col))) { 
     col++; 
    } 
    if (col > 0) { 
     r = s2.substring(0, col); // get the digits off the front 
    } 
} 

R將是你想要的字符串,或者也將是無效的,如果沒有號碼被發現。

+0

+1,因爲這可能比正則表達式更快,但是如果情況改變(假設您知道正則表達式),則正則表達式可能更直接並且更易於修改。 –

+0

@ increment1你是真的。因爲它簡單... a)用indexOf找到開始,b)找到數字序列結束的地方,c)得到字符串,我喜歡這個。在現實生活中,我會首先找到兩個列號,並執行一次'substring'調用以避免創建臨時字符串。這是一個古老的習慣。 –

2

您應該使用這個regular expression

String str = "mombojumbomombojumbomombojumbomombojumbomombojumbomombojumbohello=1fdjaslkd;fdsjaflkdjfdklsa;fjdklsa;djsfklsa;dfjklds;afj=124214fdsamf=352"; 
    Pattern p = Pattern.compile("hello=(\\d+)"); 
    Matcher m = p.matcher(str); 
    if (m.find()) { 
     System.out.println(m.group(1)); // prints 1 
    } 
0

這裏是另一個非正則表達式的表現方式。裹的方法爲您提供方便

Helper方法

public static Integer getIntegerForKey(String key, String s) 
{ 
    int startIndex = s.indexOf(key); 

    if (startIndex == -1) 
     return null; 

    startIndex += key.length(); 

    int endIndex = startIndex; 

    int len = s.length(); 

    while(endIndex < len && Character.isDigit(s.charAt(endIndex))) { 
     ++endIndex; 
    } 

    if (endIndex > startIndex) 
     return new Integer(s.substring(startIndex, endIndex)); 

    return null; 
} 

使用

Integer result = getIntegerForKey("hello=", yourInputString); 

if (result != null) 
    System.out.println(result); 
else 
    System.out.println("Key-integer pair not found."); 
0

又一非正則表達式的解決方案:

String str = "mombojumbomombojumbomombojumbomombojumbomombojumbomombojumbohello=142?fdjaslkd;fdsjaflkdjfdklsa;fjdklsa;djsfklsa;dfjklds;afj=124214fdsamf=352"; 
char arr[] = str.substring(str.indexOf("hello=")+6).toCharArray(); 
String buff =""; 
int i=0; 
while(Character.isDigit(arr[i])){ 
    buff += arr[i++]; 
} 
int result = Integer.parseInt(buff); 
System.out.println(result); 
+0

我剛剛意識到這與李·梅多爾的基本相同 – jsedano