我需要在字符串列表中的特定字符之后修饰整数



我这里有个问题。我需要从一个字符串列表中得到所有的数字

假设列表中的一个字符串是"Jhon [B] - 14,15,16 ">字符串的格式是固定的,每个字符串最多包含7个数字,数字之间用","分隔。. 我想要得到之后的所有数字"-">. 我真的很困惑,我试了我所知道的一切,但我一点也不接近。

public static List<String> readInput() {
final Scanner scan = new Scanner(System.in);
final List<String> items = new ArrayList<>();
while (scan.hasNextLine()) {
items.add(scan.nextLine());
}
return items;
}
public static void main(String[] args) {
final List<String> stats= readInput();

}

}

你可以…

使用String#indexOfString#split(和String#trim)之类的东西手动解析String

String text = "Jhon [B] - 14, 15, 16";
int indexOfDash = text.indexOf("-");
if (indexOfDash < 0 && indexOfDash + 1 < text.length()) {
return;
}
String trailingText = text.substring(indexOfDash + 1).trim();
String[] parts = trailingText.split(",");
// There's probably a really sweet and awesome
// way to use Streams, but the point is to try
// and keep it simple 😜
List<Integer> values = new ArrayList<>(parts.length);
for (int index = 0; index < parts.length; index++) {
values.add(Integer.parseInt(parts[index].trim()));
}
System.out.println(values);

打印

[14, 15, 16]
<标题>

你可以…使用自定义的Scanner分隔符,例如…

String text = "Jhon [B] - 14, 15, 16";
Scanner parser = new Scanner(text);
parser.useDelimiter(" - ");
if (!parser.hasNext()) {
// This is an error
return;
}
// We know that the string has leading text before the "-"
parser.next();
if (!parser.hasNext()) {
// This is an error
return;
}
String trailingText = parser.next();
parser = new Scanner(trailingText);
parser.useDelimiter(", ");
List<Integer> values = new ArrayList<>(8);
while (parser.hasNextInt()) {
values.add(parser.nextInt());
}
System.out.println(values);

打印……

[14, 15, 16]

或者您可以使用从字符串中提取有符号或无符号整数或浮点数的方法。下面的方法使用了string# replaceAll()方法:

/**
* This method will extract all signed or unsigned Whole or floating point 
* numbers from a supplied String. The numbers extracted are placed into a 
* String[] array in the order of occurrence and returned.<br><br>
* 
* It doesn't matter if the numbers within the supplied String have leading 
* or trailing non-numerical (alpha) characters attached to them.<br><br>
* 
* A Locale can also be optionally supplied so to use whatever decimal symbol 
* that is desired otherwise, the decimal symbol for the system's current 
* default locale is used. 
* 
* @param inputString (String) The supplied string to extract all the numbers 
* from.<br>
* 
* @param desiredLocale (Optional - Locale varArgs) If a locale is desired for a 
*               specific decimal symbol then that locale can be optionally 
*               supplied here. Only one Locale argument is expected and used 
*               if supplied.<br>
* 
* @return (String[] Array) A String[] array is returned with each element of 
*               that array containing a number extracted from the supplied 
*               Input String in the order of occurrence.
*/
public static String[] getNumbersFromString(String inputString, java.util.Locale... desiredLocale) {
// Get the decimal symbol the the current system's locale.
char decimalSeparator = new java.text.DecimalFormatSymbols().getDecimalSeparator();

/* Is there a supplied Locale? If so, set the decimal 
separator to that for the supplied locale       */
if (desiredLocale != null && desiredLocale.length > 0) {
decimalSeparator = new java.text.DecimalFormatSymbols(desiredLocale[0]).getDecimalSeparator();
} 
/* The first replaceAll() removes all dashes (-) that are preceeded
or followed by whitespaces. The second replaceAll() removes all
periods from the input string except those that part of a floating 
point number. The third replaceAll() removes everything else except 
the actual numbers. */
return  inputString.replaceAll("\s*\-\s{1,}","")
.replaceAll("\.(?![\d](\.[\d])?)", "")
.replaceAll("[^-?\d+" + decimalSeparator + "\d+]", " ")
.trim().split("\s+");
}

最新更新