如何拆分 java 中的正负数字字符串?

How do I split the plus minus number string in java?

输入:

String arrr[] = new String[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

int nostrt = -1, m = 0;
StringBuilder str4 = new StringBuilder();
for(int i = 0;i < strbullist.length(); i++)
{    
    char c = strbullist.charAt(i);

    if(nostrt == -1)
    {
        m = i;
        nostrt = 1; 
    }
    if(c=='-')
    {
        
        str4.append(strbullist.substring(m, i));
        nostrt = -1;
        System.out.println(str4);
    }    
}

试试这个逻辑并根据您的需要进行更改。

进口java.util.Scanner; public class p19 {

 public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter the size of the array:");
     int size;
     size=sc.nextInt();
     int arr[ ]=new int[size];
  int i,j=0;
  System.out.println("Enter the Element of the array:");
      while(j<size)
     {
         arr[j]=sc.nextInt();
         j++;
     }
      System.out.println("Positive numbers are:");
      for(i=0;i<size;i++)
      {
          if(arr[i]>0)
          {
           System.out.print(arr[i]+" ");
          }
      }

      System.out.println("\nNegative numbers are:");
      for(i=0;i<size;i++){
          if(arr[i]<0)
          {
           System.out.print(arr[i]+" ");
          }
      }
     sc.close();
     }}

您可以将字符串拆分为一个或多个空白字符(即 \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+ which means 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

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

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter size : ");
        int size = sc.nextInt();
        
        String [] numbers = new String[size];
        String positive = "", negative = "";
        
        System.out.print("Enter number with signs (-) or (+) : ");
        for(int i = 0; i < size; i++){
            numbers[i] = sc.next();
        }
        
        for(int i =0; i < size; i++){
            if(numbers[i].contains("-")){
                negative += numbers[i] + " ";
            }else if(numbers[i].contains("+")){
                positive += numbers[i] + " ";
            }
        }
        
        System.out.println("Negatives : " + negative);
        System.out.println("Positives : " + positive);