2013-12-19 79 views
4

我在Java字符串中有一些表達式,並且會得到字母,它位於特定符號的左側和右側。如何使用java提取字符串中的特定字母

下面是兩個例子:

X-Y 
X-V-Y 

現在,我需要在第一個例子中,以提取字母X和Y在一個單獨的字符串。在第二個例子中,我需要在單獨的字符串中提取字母X,V和Y.

我該如何在Java中實現這個需求?

+1

使用'split'方法。 –

+1

查看StringTokenizer – hovanessyan

+0

到目前爲止,您已經發布了47個問題,其中大部分有1個或更多答案。如果你願意,你可以自由接受。但我認爲你應該接受他們中的一些,並最終獲得學者徽章:) – giampaolo

回答

1

嘗試使用:

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

使用String.split方法與"-"令牌

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

現在輸出數組中會有3元件X,Y,V

1
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

您可以使用此方法:

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