您可以将字符串拆分为一个或多个空白字符(即\s+)并迭代生成的数组以查找元素是以正符号还是负符号开头。
演示:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
List<String> positiveNums = new ArrayList<>();
List<String> negativeNums = new ArrayList<>();
String[] arr = str.split("\\s+");
for (String s : arr) {
if (s.startsWith("+")) {
positiveNums.add(s);
} else if (s.startsWith("-")) {
negativeNums.add(s);
}
}
System.out.println("Positive numbers: " + positiveNums);
System.out.println("Negative numbers: " + negativeNums);
// If you want to store the output into string variables
String positiveValues = positiveNums.toString();
String negativeValues = negativeNums.toString();
System.out.println("Positive numbers: " + positiveValues);
System.out.println("Negative numbers: " + negativeValues);
}
}
输出:
Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]
Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]
或者,您也可以使用regex、\+\d+|\-\d+,这意味着one or more 数字后跟一个加号(即\+\d+)或(即|)一个或多个数字后跟一个负号(即\-\d+)。
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
Matcher matcher = Pattern.compile("\\+\\d+|\\-\\d+").matcher(str);
List<String> positiveNums = new ArrayList<>();
List<String> negativeNums = new ArrayList<>();
while (matcher.find()) {
String s = matcher.group();
if (s.startsWith("+")) {
positiveNums.add(s);
} else if (s.startsWith("-")) {
negativeNums.add(s);
}
}
System.out.println("Positive numbers: " + positiveNums);
System.out.println("Negative numbers: " + negativeNums);
}
}
输出:
Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]
如果您想使用StringBuilder 而不是List:
public class Main {
public static void main(String[] args) {
String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
StringBuilder positiveNums = new StringBuilder();
StringBuilder negativeNums = new StringBuilder();
String[] arr = str.split("\\s+");
for (String s : arr) {
if (s.startsWith("+")) {
positiveNums.append(s + " ");
} else if (s.startsWith("-")) {
negativeNums.append(s + " ");
}
}
String positiveValues = positiveNums.toString().trim();
String negativeValues = negativeNums.toString().trim();
System.out.println("Positive numbers: " + positiveValues);
System.out.println("Negative numbers: " + negativeValues);
}
}
输出:
Positive numbers: +2501 +2502 +2503 +2504 +2505 +2506 +2507 +2509 +2501 +2511 +2513
Negative numbers: -2501 -2504 -2502 -2505 -2507 -2503 -2511 -2509
使用String#matches:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
List<String> positiveNums = new ArrayList<>();
List<String> negativeNums = new ArrayList<>();
String[] arr = str.split("\\s+");
for (String s : arr) {
if (s.matches("\\+\\d+")) {
positiveNums.add(s);
} else if (s.matches("\\-\\d+")) {
negativeNums.add(s);
}
}
System.out.println("Positive numbers: " + positiveNums);
System.out.println("Negative numbers: " + negativeNums);
}
}
输出:
Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]
更新(基于更新的问题):
如果您问题中的字符串是String[] 的元素,则需要一个额外的循环来迭代此String[]。其余的将保持不变。
public class Main {
public static void main(String... args) {
String[] strArr = { "+2501 +2502 +2503 +2504", "-2501 -2504 +2505 +2506 +2507 +2509", "+2501 +2511 -2502 -2505",
"+2513 -2507 -2503 -2511 -2509" };
StringBuilder positiveNums = new StringBuilder();
StringBuilder negativeNums = new StringBuilder();
for (String str : strArr) {
String[] arr = str.split("\\s+");
for (String s : arr) {
if (s.startsWith("+")) {
positiveNums.append(s + " ");
} else if (s.startsWith("-")) {
negativeNums.append(s + " ");
}
}
}
String positiveValues = positiveNums.toString().trim();
String negativeValues = negativeNums.toString().trim();
System.out.println("Positive numbers: " + positiveValues);
System.out.println("Negative numbers: " + negativeValues);
}
}
输出:
Positive numbers: +2501 +2502 +2503 +2504 +2505 +2506 +2507 +2509 +2501 +2511 +2513
Negative numbers: -2501 -2504 -2502 -2505 -2507 -2503 -2511 -2509