How to extract specific letters in a string using java

I have some expression in a Java string, and a will get the letter that was to the left and right of the specific character.

Here are two examples:

X-Y
X-V-Y

Now I need to extract the letter X and Y in a separate line in the first example. And in the second example, I need to extract the letters X, V and Y on a separate line.

How can I implement this requirement in Java?

+4
source share
7 answers

Try:

String input    = "X-V-Y";
String[] output = input.split("-");  // array with: "X", "V", "Y"
+1
source

use String.split with "-"token

String input = "X-Y-V"
String[] output = input.split("-");

now in the output array there will be three elements X, Y, V

+1
source
String[] results = string.split("-");
+1

String input ="X-V-Y";
String[] arr=input.split("-");

arr[0]-->X
arr[1]-->V
arr[2]-->Y
+1

!

String[] terms = str.split("\\W"); // split on non-word chars
+1

, :

String input = "x-v-y";
String[] extractedInput = intput.split("-");
for (int i = 0; i < extractedInput.length - 1; i++) {
    System.out.print(exctractedInput[i]);
}

: xvy

+1

You can use this method:

public String[] splitWordWithHyphen(String input) {
    return input.split("-");
}
+1
source

All Articles