の間の単語を置き換え、私は 私は、Javaの初心者です文字
以下flybirdy_blue.co
strongwolf_red.po
を持っている私は
blue
red
String[] parts = csvFile.split("_");
String color = parts[1];
、このような結果をしたいが、それは私に間違った結果を与える
の間の単語を置き換え、私は 私は、Javaの初心者です文字
以下flybirdy_blue.co
strongwolf_red.po
を持っている私は
blue
red
String[] parts = csvFile.split("_");
String color = parts[1];
、このような結果をしたいが、それは私に間違った結果を与える
は、あなたが「_」との間の値を取得するには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);
出力
青
赤
downvoteなぜ? :S –
これを手伝ってもらえますか?もしそれが 'fly_birdy_blue.co'だったらどうすればいいのですか?結果として青が欲しいです – Moudiz
別の質問をしてください –
あなたは"_"の周りにプリーツしますが、さらに ""を分割する必要があります。 ..
ので
String[] parts = csvFile.split("_");
String color = parts[1].split(".")[1];
それとも、周り_ &分割しようとすることができ、してみてください。使用して同時に「[]」「文字クラス」の項でhereを説明した:「」
String[] parts = csvFile.split("[_.]");
String color = parts[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
}
を
たぶん、あなたはより良く理解するために、独自の関数を記述する必要があります
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;
}
パーツを[1] .split( "") – mehulmpt