如何在java中拆分正负号字符串?

lvmkulzt  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(493)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

17天前关门了。
改进这个问题
输入:
字符串arrr[]=新字符串[4];
arrr[0]=+2501+2502+2503+2504
arrr[1]=-2501-2504+2505+2506+2507+2509
arrr[2]=+2501+2511-2502-2505
arrr[3]=+2513-2507-2503-2511-2509
输出:
我要将字符串分隔为:
肯定的:
arrr1[0]=+2501+2502+2503+2504
arrr1[1]=+2505+2506+2507+2509
arrr1[2]=+2501+2511
arrr1[3]=+2513
阴性:
arrr2[0]=-2501-2504
arrr2[1]=-2502-2505
arrr2[2]=-2507-2503-2511-2509

  1. int nostrt = -1, m = 0;
  2. StringBuilder str4 = new StringBuilder();
  3. for(int i = 0;i < strbullist.length(); i++)
  4. {
  5. char c = strbullist.charAt(i);
  6. if(nostrt == -1)
  7. {
  8. m = i;
  9. nostrt = 1;
  10. }
  11. if(c=='-')
  12. {
  13. str4.append(strbullist.substring(m, i));
  14. nostrt = -1;
  15. System.out.println(str4);
  16. }
  17. }
svdrlsy4

svdrlsy41#

可以将字符串拆分为一个或多个空格字符(即。 \s+ )并迭代得到的数组,以查找元素是以正符号还是以负符号开头。
演示:

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class Main {
  4. public static void main(String[] args) {
  5. String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
  6. List<String> positiveNums = new ArrayList<>();
  7. List<String> negativeNums = new ArrayList<>();
  8. String[] arr = str.split("\\s+");
  9. for (String s : arr) {
  10. if (s.startsWith("+")) {
  11. positiveNums.add(s);
  12. } else if (s.startsWith("-")) {
  13. negativeNums.add(s);
  14. }
  15. }
  16. System.out.println("Positive numbers: " + positiveNums);
  17. System.out.println("Negative numbers: " + negativeNums);
  18. // If you want to store the output into string variables
  19. String positiveValues = positiveNums.toString();
  20. String negativeValues = negativeNums.toString();
  21. System.out.println("Positive numbers: " + positiveValues);
  22. System.out.println("Negative numbers: " + negativeValues);
  23. }
  24. }

输出:

  1. Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
  2. Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]
  3. Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
  4. Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]

或者,也可以使用regex, \+\d+|\-\d+ 表示一个或多个数字后跟加号(即。 \+\d+ )或(即。 | )一个或多个数字后跟一个负号(即。 \-\d+ ).

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. public class Main {
  6. public static void main(String[] args) {
  7. String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
  8. Matcher matcher = Pattern.compile("\\+\\d+|\\-\\d+").matcher(str);
  9. List<String> positiveNums = new ArrayList<>();
  10. List<String> negativeNums = new ArrayList<>();
  11. while (matcher.find()) {
  12. String s = matcher.group();
  13. if (s.startsWith("+")) {
  14. positiveNums.add(s);
  15. } else if (s.startsWith("-")) {
  16. negativeNums.add(s);
  17. }
  18. }
  19. System.out.println("Positive numbers: " + positiveNums);
  20. System.out.println("Negative numbers: " + negativeNums);
  21. }
  22. }

输出:

  1. Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
  2. Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]

如果你想用 StringBuilder 而不是 List :

  1. public class Main {
  2. public static void main(String[] args) {
  3. String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
  4. StringBuilder positiveNums = new StringBuilder();
  5. StringBuilder negativeNums = new StringBuilder();
  6. String[] arr = str.split("\\s+");
  7. for (String s : arr) {
  8. if (s.startsWith("+")) {
  9. positiveNums.append(s + " ");
  10. } else if (s.startsWith("-")) {
  11. negativeNums.append(s + " ");
  12. }
  13. }
  14. String positiveValues = positiveNums.toString().trim();
  15. String negativeValues = negativeNums.toString().trim();
  16. System.out.println("Positive numbers: " + positiveValues);
  17. System.out.println("Negative numbers: " + negativeValues);
  18. }
  19. }

输出:

  1. Positive numbers: +2501 +2502 +2503 +2504 +2505 +2506 +2507 +2509 +2501 +2511 +2513
  2. Negative numbers: -2501 -2504 -2502 -2505 -2507 -2503 -2511 -2509

使用 String#matches :

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class Main {
  4. public static void main(String[] args) {
  5. String str = "+2501 +2502 +2503 +2504 -2501 -2504 +2505 +2506 +2507 +2509 +2501 +2511 -2502 -2505 +2513 -2507 -2503 -2511 -2509";
  6. List<String> positiveNums = new ArrayList<>();
  7. List<String> negativeNums = new ArrayList<>();
  8. String[] arr = str.split("\\s+");
  9. for (String s : arr) {
  10. if (s.matches("\\+\\d+")) {
  11. positiveNums.add(s);
  12. } else if (s.matches("\\-\\d+")) {
  13. negativeNums.add(s);
  14. }
  15. }
  16. System.out.println("Positive numbers: " + positiveNums);
  17. System.out.println("Negative numbers: " + negativeNums);
  18. }
  19. }

输出:

  1. Positive numbers: [+2501, +2502, +2503, +2504, +2505, +2506, +2507, +2509, +2501, +2511, +2513]
  2. Negative numbers: [-2501, -2504, -2502, -2505, -2507, -2503, -2511, -2509]

更新(基于更新的问题):

如果问题中的字符串是 String[] ,您需要一个额外的循环来迭代 String[] . 其余的事情将保持不变。

  1. public class Main {
  2. public static void main(String... args) {
  3. String[] strArr = { "+2501 +2502 +2503 +2504", "-2501 -2504 +2505 +2506 +2507 +2509", "+2501 +2511 -2502 -2505",
  4. "+2513 -2507 -2503 -2511 -2509" };
  5. StringBuilder positiveNums = new StringBuilder();
  6. StringBuilder negativeNums = new StringBuilder();
  7. for (String str : strArr) {
  8. String[] arr = str.split("\\s+");
  9. for (String s : arr) {
  10. if (s.startsWith("+")) {
  11. positiveNums.append(s + " ");
  12. } else if (s.startsWith("-")) {
  13. negativeNums.append(s + " ");
  14. }
  15. }
  16. }
  17. String positiveValues = positiveNums.toString().trim();
  18. String negativeValues = negativeNums.toString().trim();
  19. System.out.println("Positive numbers: " + positiveValues);
  20. System.out.println("Negative numbers: " + negativeValues);
  21. }
  22. }

输出:

  1. Positive numbers: +2501 +2502 +2503 +2504 +2505 +2506 +2507 +2509 +2501 +2511 +2513
  2. Negative numbers: -2501 -2504 -2502 -2505 -2507 -2503 -2511 -2509
展开查看全部
rqmkfv5c

rqmkfv5c2#

试试这个逻辑,根据你的需要改变。
导入java.util.scanner;公共课p19{

  1. public static void main(String[] args) {
  2. Scanner sc=new Scanner(System.in);
  3. System.out.println("Enter the size of the array:");
  4. int size;
  5. size=sc.nextInt();
  6. int arr[ ]=new int[size];
  7. int i,j=0;
  8. System.out.println("Enter the Element of the array:");
  9. while(j<size)
  10. {
  11. arr[j]=sc.nextInt();
  12. j++;
  13. }
  14. System.out.println("Positive numbers are:");
  15. for(i=0;i<size;i++)
  16. {
  17. if(arr[i]>0)
  18. {
  19. System.out.print(arr[i]+" ");
  20. }
  21. }
  22. System.out.println("\nNegative numbers are:");
  23. for(i=0;i<size;i++){
  24. if(arr[i]<0)
  25. {
  26. System.out.print(arr[i]+" ");
  27. }
  28. }
  29. sc.close();
  30. }}
展开查看全部
u4dcyp6a

u4dcyp6a3#

如果你想把数字存储在一个字符串中,试试这个

  1. Scanner sc = new Scanner(System.in);
  2. System.out.print("Enter size : ");
  3. int size = sc.nextInt();
  4. String [] numbers = new String[size];
  5. String positive = "", negative = "";
  6. System.out.print("Enter number with signs (-) or (+) : ");
  7. for(int i = 0; i < size; i++){
  8. numbers[i] = sc.next();
  9. }
  10. for(int i =0; i < size; i++){
  11. if(numbers[i].contains("-")){
  12. negative += numbers[i] + " ";
  13. }else if(numbers[i].contains("+")){
  14. positive += numbers[i] + " ";
  15. }
  16. }
  17. System.out.println("Negatives : " + negative);
  18. System.out.println("Positives : " + positive);
展开查看全部

相关问题