伙計們,我在這里遇到了問題。我需要從字串串列中的字串中獲取所有數字。
假設串列中的一個字串是"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();
}
}
uj5u.com熱心網友回復:
你可以...
只需手動決議String使用String#indexOf和String#split(和String#trim)之類的東西
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]
uj5u.com熱心網友回復:
您可以使用簡單的字串操作,例如:
public static List<String> readInput() {
String text = "Jhon [B] - 14, 15, 16";
String numbersOnly = text.substring(text.indexOf("-") 1).trim();
return Arrays.stream(numbersOnly.split(",")).collect(Collectors.toList());
}
public static void main(String[] args) {
final List<String> stats = readInput();
}
uj5u.com熱心網友回復:
或者您可以使用從字串中提取有符號或無符號整數或浮點數的方法。下面的方法使用了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("\\.(??)", "")
.replaceAll("[^-?\\d " decimalSeparator "\\d ]", " ")
.trim().split("\\s ");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/467314.html
