user2504767
user2504767

Reputation:

How to extract specific letters in string with java

I have some expression in a Java string and a would get the letter, which was on the left side and on the right side of a specific sign.

Here are two examples:

X-Y
X-V-Y

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

How can i implemented this requirement in Java?

Upvotes: 5

Views: 1102

Answers (7)

Deva
Deva

Reputation: 2536

you can extract and handle a string by using the following code:

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

Output: xvy

Upvotes: 1

Sujith PS
Sujith PS

Reputation: 4864

You can use this method :

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

Upvotes: 1

Bohemian
Bohemian

Reputation: 425013

I'm getting in on this too!

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

Upvotes: 1

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

do like this

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

output

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

Upvotes: 1

Lukas Eichler
Lukas Eichler

Reputation: 5903

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

Upvotes: 1

stinepike
stinepike

Reputation: 54672

use String.split method with "-" token

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

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

Upvotes: 1

hsz
hsz

Reputation: 152216

Try with:

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

Upvotes: 1

Related Questions