2013-08-20 149 views
0

標題似乎很簡單。但我沒有一個好主意。情況是這樣的分割Java字符串

我有字符串像這樣在我的Java程序

String scz="3282E81WHT-22/24"; 

我想上面的字符串分成3個字符串,這樣

第一個字符串值應爲3282e81

接着字符串應該是WHT(即,上面的字符串和this part is Always of 3 Characters的字符串部分),

下一個字符串值應爲22/24Which will always occur after -

總之

String first= /* do some expression on scz And value should be "3282e81" */; 
String second= /* do some expression on scz And value should be "WHT" */; 
String third= /* do some expression on scz And value should be "22/24" */; 

輸入,也可以像

scz="324P25BLK-12"; 

所以324P25將是第一字符串, BLK將是第二個(of 3 Characters)。 將第三(After - symbol)

如何解決這個問題?

+0

「WHT」部分是否固定?我的意思是,你想用「WHT」來分割還是還會有其他人物? –

+4

你需要向後退一步。爲什麼第一個元素'3282E81'第二個元素'WHT'和第三個元素'22/24'?只有當你知道規則時,你才能實施它們。 – BevynQ

+0

不會,它會改變。但那部分將是String。 它也可以像這樣 'scz = 324P25BLK-12' 所以** 324P25 **將會是第一個字符串,** BLK **會是第二個。 ** 12 **將是第三個 – iCode

回答

1

如果字符串的第二部分(WHT)等永遠是3個字符,然後下面的代碼一定會幫助你

String scz = "3282E81WHT-22/24"; 

      String Third[] = scz.split("-"); 
      String rev = new StringBuilder(Third[0]).reverse().toString(); 
      String Second=rev.substring(0,3); 
      String First=rev.substring(3,rev.length()); 

      // here Reverse your String Back to Original 
      First=new StringBuilder(First).reverse().toString(); 
      Second=new StringBuilder(Second).reverse().toString(); 

      System.out.println(First + " " + Second + " " + Third[1]); 
+0

我得到的輸出是這樣的 '18E2823 THW 22/24'前兩個是相反的 – iCode

+0

@Shiju ohh對不起更新回答! –

1

你可以使用subString()方法來獲得這個目標。 subString有重載次數。

的第一串

String first=scz.subString(0,6); 
String second=scz.subString(7,9); 
+0

但這是基於長度。字符串值的長度可能會改變 – iCode

1

您可以使用下面的正則表達式來取出上述類型的字符串:

Pattern pattern = Pattern.compile("\\d+[A-Z]\\d{2}|[A-Z]{3}|(?<=-)[\\d/]+"); 
    Matcher matcher = pattern.matcher("3282E81WHT-22/24"); 
    while (matcher.find()) { 
     System.out.println(matcher.group()); 
    } 

\d+[A-Z]\d{2}|[A-Z]{3}|(?<=-)[\d/]+ 

在Java中,您可以按以下方式使用上述正則表達式

輸出:

3282E81 
WHT 
22/24 
0

如果字符串的長度是固定的溶劑淨化,第一,第二和第三的則可以使用

String first=scz.subString(0,6); 
    String second=scz.subString(7,9); 
    String third=scz.subString(10,scz.length()); 
4

您可以使用正則表達式這樣(\d+[A-Z]\d+)([A-Z]+)-([/\d]+)和使用Matcher.group(int)方法,你可以讓你的字符串分裂成三組。

代碼片斷

String str = "3282E81WHT-22/24"; 
//str = "324P25BLK-12"; 
Pattern pattern = Pattern.compile("(\\d+[A-Z]\\d+)([A-Z]+)-([/\\d]+)"); 
Matcher match = pattern.matcher(str); 
System.out.println(match.matches()); 
System.out.println(match.group(1)); 
System.out.println(match.group(2)); 
System.out.println(match.group(3)); 

輸出

true 
3282E81 
WHT 
22/24 
0
String scz="3282E81WHT-22/24"; 
String[] array = scz.split("-"); 
String str1 = (String) array[0].subSequence(0, 7); 
String str2 = array[0].substring(7); 

然後拆分將在此爲了:)

str1 
str2 
array[1] 
2

使用此功能可以在兩個

String[] parts = issueField.split("-"); 
String first = parts[0]; 
String second= parts[1]; 

使用此整個字符串分割到第一串分成兩個

if (first!=null && first.length()>=3){ 
    String lastThree=first.substring(first.length()-3); 
} 
0

您可以使用字符數組而不是字符串,以便您可以訪問數組中的特定字符。 實施例

char scz[] = "3282E81WHT-22/24"; 

,只是通過指定其中陣列要使用的地方訪問單獨的字符。

0

你可以試試這個

 String scz="3282E81WHT-22/24"; 
     String[] arr=scz.split("-"); 
     System.out.println("first: "+arr[0].substring(0,7)); 
     System.out.println("second: "+arr[0].substring(7,10)); 
     System.out.println("third: "+arr[1]) 
0

看看我的解決方案 -

class Main { 
    public static void main(String[] args) { 
      String first = ""; 
      String second = ""; 
      String third = ""; 

      String scz="3282E81WHT-22/24"; 
      String[] portions = scz.split("-"); 
      if (portions.length > 1) { 
        third = portions[1]; 
      } 

      String[] anotherPortions = portions[0].split("[a-zA-Z]+$"); 
      if (anotherPortions.length > 0) { 
        first = anotherPortions[0]; 
      } 

      second = portions[0].substring(first.length()); 

      System.out.println(first); 
      System.out.println(second); 
      System.out.println(third); 
    } 
} 

Live Demo