2013-03-20 5 views
3
String weapon = "pistol . <1/10>"; 
Result: 
int clip = 1; 
int ammo = 1; 

문자열의 형식 = (WEAPON). < (CLIP)/(AMMO)>
그리고 클립과 탄약 값이 필요합니다.문자열에서 2 정수를 추출하십시오.

그래서 어떻게 문자열의 두 값을 추출 할 수 있습니다. 는 아직도 직면하게 될 것이다 "/"그것으로하여 분할 할 수 있습니다 : "총 < 1"과 "10">
감사를 사전

+0

http://docs.oracle.com/ javase/1.5.0/docs/api/java/lang/String.html # substring (int, int) – rajesh

답변

6

는이 같은 비 숫자 문자를 제거 할 수 있습니다

String s = "pistol . <1/10>"; 
String[] numbers = s.replaceAll("^\\D+","").split("\\D+"); 

지금 numbers[0]1numbers[1]10입니다. 새 문자열 지금 있도록
  • s.replaceAll("^\\D+","")

    은 문자열의 시작 부분에 비 숫자 문자를 제거 "1/10>"
  • .split("\\D+") (이 경우 />에서) 비 숫자 문자에 분할 및 후행 빈 문자열의 경우를 무시 당신이 당신의 질문에서 언급 한 형식이 정확히 항상있는 경우

또는

, 당신은 특정 patte를 찾을 수 RN :

private final static Pattern CLIP_AMMO = Pattern.compile(".*<(\\d+)/(\\d+)>.*"); 

String s = "pistol . <1/10>"; 
Matcher m = CLIP_AMMO.matcher(s); 
if (m.matches()) { 
    String clip = m.group(1); //1 
    String ammo = m.group(2); //10 
} 
+0

고마워, 무기 이름을 얻을 가능성이 있습니까? 나는 2 개의 정수와 1 개의 문자열을 가지고 있습니다.) – user1621988

+0

@ user1621988 두 번째 접근법은 다음과 같습니다 :'Pattern p = Pattern.compile ("\\ b (\\ w +) \\ b. * (\\ d +)/(\\ d +)>. *"); 무기는'm.group (1)', 클립은'm.group (2)'및 탄약'm.group (3) '이됩니다. – assylias

0

을에 그리고 u는 "<"(총 < 1)에 의해 생성 된 문자열을 분할 및 ">"(10>) 각각

1
String weapon = "pistol . <1/10>"; 

String str = weapon.substring(weapon.indexOf("<")+1,weapon.indexOf(">")); // str = "<1/10>" 

int clip = Integer.parseInt(str.substring(0,str.indexOf("/"))); // clip = 1 

int ammo = Integer.parseInt(str.substring(str.indexOf("/")+1)); // ammo = 10 

클립 = 1 탄약 10

가 완료 =

..

1

당신이 시도 할 수도 ...

문자열 "(무기)의 값 무기, CLIP 및 탄약을 추출. 권총의 "의 값 (1), 문자열 (10)을 추출

String str = "(WEAPON) . <(CLIP)/(AMMO)>"; 
Pattern pattern = Pattern.compile("\\((.*?)\\)"); 
Matcher matcher = pattern.matcher(str); 
while(matcher.find()) { 
    System.out.println(matcher.group(1)); 
} 

"< (CLIP)/(AMMO)>. < 1/10> ".

List<String[]> numbers = new ArrayList<String[]>(); 
String str = "pistol . <1/10>"; 
Pattern pattern = Pattern.compile("\\<(.*?)\\>"); 
Matcher matcher = pattern.matcher(str); 
while(matcher.find()) { 
    numbers.add(matcher.group(1).split("/")); 
} 
0

당신은 이런 식으로 뭔가를 시도 할 수 있습니다

public static void main(String[] args) { 
    String weapon = "pistol . <1/10>"; 
    String test = ""; 

    Pattern pattern = Pattern.compile("<.*?>"); 
    Matcher matcher = pattern.matcher(weapon); 

    if (matcher.find()) 
     test = matcher.group(0); 

    test = test.replace("<", "").replace(">", ""); 
    String[] result = test.split("/"); 

    int clip = Integer.parseInt(result[0]); 
    int ammo = Integer.parseInt(result[1]); 
    System.out.println("Clip value is -->" + clip + "\n" 
      + "Ammo value is -->" + ammo); 
} 

출력 :

Clip value is -->1 
Ammo value is -->10 
1
String weapon = "pistol . <1/10>"; 
Pattern p = Pattern.compile("\\d+"); 
Matcher m = p.matcher(weapon); 
List<Integer> numbers = new ArrayList<Integer>(); 
while (m.find()) { 
    numbers.add(Integer.parseInt(m.group())); 
} 
System.out.println("CLIP: " + numbers.get(0)); 
System.out.println("AMMO: " + numbers.get(1)); 
관련 문제