简体   繁体   English

如何用java提取字符串中的特定字母

[英]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. 我在Java字符串中有一些表达式,并且会得到字母,该字母位于特定符号的左侧和右侧。

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. 现在,我需要在第一个例子中提取单独字符串中的字母X和Y. And in the second example, I need to extract the letters X, V and Y in a separate string. 在第二个例子中,我需要在一个单独的字符串中提取字母X,V和Y.

How can i implemented this requirement in Java? 我如何在Java中实现此要求?

Try with: 试试:

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

use String.split method with "-" token 使用带有"-"标记的String.split方法

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

now in the output array there will be 3 elements X,Y,V 现在在输出数组中将有3个元素X,Y,V

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

do like this 这样做

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

output 产量

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

我也加入了这个!

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

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 输出:xvy

You can use this method : 您可以使用此方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM