2017-03-15 7 views
-2

の間の単語を置き換え、私は 私は、Javaの初心者です文字

以下
flybirdy_blue.co 

strongwolf_red.po 

を持っている私は

blue
red

String[] parts = csvFile.split("_"); 
       String color = parts[1]; 

、このような結果をしたいが、それは私に間違った結果を与える

+0

パーツを[1] .split( "") – mehulmpt

答えて

0

は、あなたが「_」との間の値を取得するにはsubstring()機能を使用することができます次のように。

ここに行く:

String firstcsvFile = "flybirdy_blue.co"; 
String secondcsvFile = "strongwolf_red.po"; 
String result = firstcsvFile.substring(firstcsvFile.indexOf("_") + 1, firstcsvFile.indexOf(".")); 
String result2 = secondcsvFile.substring(secondcsvFile.indexOf("_") + 1, secondcsvFile.indexOf(".")); 
System.out.println(result); 
System.out.println(result2); 

出力

+0

downvoteなぜ? :S –

+0

これを手伝ってもらえますか?もしそれが 'fly_birdy_blue.co'だったらどうすればいいのですか?結果として青が欲しいです – Moudiz

+0

別の質問をしてください –

1

あなたは"_"の周りにプリーツしますが、さらに ""を分割する必要があります。 ..

ので

String[] parts = csvFile.split("_"); 
String color = parts[1].split(".")[1]; 

それとも、周り_ &分割しようとすることができ、してみてください。使用して同時に「[]」「文字クラス」の項でhereを説明した:「」

String[] parts = csvFile.split("[_.]"); 
String color = parts[1]; 
1

あなたがそれを使用することができます。

String s = "flybirdy_blue.po"; 
Pattern pattern = Pattern.compile("(_)(.+)(\\.)"); 
Matcher matcher = pattern.matcher(s); 
if (matcher.find()) { 
    System.out.println(matcher.group(2)); //red 
} 
1

たぶん、あなたはより良く理解するために、独自の関数を記述する必要があります

public ArrayList<String> splitMyString(String wholeString, char[] splitHere){ 

    ArrayList<String> response = new ArrayList<String>(); 
    String temp =""; 
    boolean skip = false; 

    for(int i = 0 ; i < wholeString.length(); i++){ 
     for(int j = 0 ; j < splitHere.length; j++){ 
      if(wholeString.charAt(i) == splitHere[j]){ 
       response.add(temp); 
       temp=""; 
       skip = true; 
      } 
     } 
     if(skip != true){ 
      temp = temp +wholeString.charAt(i); 
     }else{ 
      skip = false; 
     } 
    } 
    response.add(temp); 
    return response; 
} 
関連する問題