Problem with java split ()

I have a line:

strArray= "-------9---------------";

I want to find 9 from a string. The line could be like this:

strArray= "---4-5-5-7-9---------------";

Now I want to know only the numbers from the string. I need a value of 9.4, or such things, and ignore the "-". I tried the following:

strArray= strignId.split("-");

but he gets an error, because there are a few "-", and I do not get my output. So which java function should I use?

My input and output should be as follows:

input="-------9---------------";
    output="9";
input="---4-5-5-7-9---------------";
    output="45579";

What should I do?

+3
source share
3 answers

You are not using split!

Split should get things between the separator.

To do this, you want to eliminate unwanted characters; '-'

The solution is simple.

from = in.replaceAll ("-", "");

+5
source

+ " " , -+ " ". str.split("-+"), .

-, str = str.replace("-", ""). replace(CharSequence, CharSequence), String, .. .

String[] , : , , , (?!^) ( ). char[], String.toCharArray()

, , java.util.regex.Matcher find() \d java.util.Scanner \D*, (, ) . , List (. Java 2nd Edition, 25: ).


, :

    System.out.println(java.util.Arrays.toString(
        "---4--5-67--8-9---".split("-+")
    ));
    // [, 4, 5, 67, 8, 9]
    // note the empty string as first element

    System.out.println(
        "---4--5-67--8-9---".replace("-", "")
    );
    // 456789

    System.out.println(java.util.Arrays.toString(
        "abcdefg".toCharArray()
    ));
    // [a, b, c, d, e, f, g]

\d, , (?!^), String[], :

    System.out.println(java.util.Arrays.toString(
        "@*#^$4@!#5ajs67>?<{8_(9SKJDH"
            .replaceAll("\\D", "")
            .split("(?!^)")
    ));
    // [4, 5, 6, 7, 8, 9]

Scanner, \D* , , List<String>:

    List<String> digits = new ArrayList<String>();
    String text = "(&*!@#123ask45{P:L6";
    Scanner sc = new Scanner(text).useDelimiter("\\D*");
    while (sc.hasNext()) {
        digits.add(sc.next());
    }
    System.out.println(digits);
    // [1, 2, 3, 4, 5, 6]

split()

String.split:

โ„–1: split

, , :

System.out.println(java.util.Arrays.toString(
    "one|two|three".split("|")
));
// [, o, n, e, |, t, w, o, |, t, h, r, e, e]

System.out.println(java.util.Arrays.toString(
    "not.like.this".split(".")
));
// []

, | . , , , , Java "\\".

System.out.println(java.util.Arrays.toString(
    "one|two|three".split("\\|")
));
// [one, two, three]

System.out.println(java.util.Arrays.toString(
    "not.like.this".split("\\.")
));
// [not, like, this]

โ„–2: split

( split):

    System.out.println(java.util.Arrays.toString(
        "a;b;;d;;;g;;".split(";")
    ));
    // [a, b, , d, , , g]

, "" c, e, f, h i. , limit String.split(String regex, int limit).

    System.out.println(java.util.Arrays.toString(
        "a;b;;d;;;g;;".split(";", -1)
    ));
    // [a, b, , d, , , g, , ]

limit of n n - 1 (.. n ).


split

; , "".

, :

    String str = "Really?Wow!This.Is.Awesome!";
    System.out.println(java.util.Arrays.toString(
        str.split("(?<=[.!?])")
    )); // prints "[Really?, Wow!, This., Is., Awesome!]"

, \G

    String str = "012345678901234567890";
    System.out.println(java.util.Arrays.toString(
        str.split("(?<=\\G.{4})")
    )); // prints "[0123, 4567, 8901, 2345, 6789, 0]"

( !)

    System.out.println(java.util.Arrays.toString(
        "OhMyGod".split("(?=(?!^)[A-Z])")
    )); // prints "[Oh, My, God]"

.

+19

- , . , / .


final Vector nodes = new Vector();
int index = original.indexOf(separator);
while (index >= 0) {
  nodes.addElement(original.substring(0, index));
  original = original.substring(index + separator.length());
  index = original.indexOf(separator);
}
nodes.addElement(original);
final String[] result = new String[nodes.size()];
if (nodes.size() > 0) {
  for (int loop = 0; loop smaller nodes.size(); loop++) {
    result[loop] = (String) nodes.elementAt(loop);
  }
}
return result;
}
0

Source: https://habr.com/ru/post/1758176/


All Articles