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

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

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

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

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);
    }    
}
svdrlsy4

svdrlsy41#

可以将字符串拆分为一个或多个空格字符(即。 \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+ 表示一个或多个数字后跟加号(即。 \+\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
rqmkfv5c

rqmkfv5c2#

试试这个逻辑,根据你的需要改变。
导入java.util.scanner;公共课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();
     }}
u4dcyp6a

u4dcyp6a3#

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

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);

相关问题