2017-01-24 67 views
-1

我輸入字符串如下:根據java中的典型條件拆分字符串?

String sample1 = "productName/@languageCode"; 
String sample1="brandNameInformation/languageSpecificBrandName/@languageCode"; 

我想只有當存在「/」 occurence分割它,而不是當「/ @」

我該怎麼辦呢? 任何人都可以幫忙嗎?

+0

'的String [] =零件sample1.split( 「/ [^ @]」);' –

+0

看一看[點擊這裏](https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html)並搜索「lookahead」。 – Mena

回答

1

您可以使用negative lookahead

import java.util.Arrays; 

public class Main16 { 
    public static void main(String[] args) { 
     String sample1 = "productName/@languageCode"; 
     String sample2 ="brandNameInformation/languageSpecificBrandName/@languageCode"; 

     String regex = "/([email protected])"; 

     System.out.println(Arrays.deepToString(sample1.split(regex))); 
     System.out.println(Arrays.deepToString(sample2.split(regex))); 
    } 
} 

輸出:

[productName/@languageCode] 
[brandNameInformation, languageSpecificBrandName/@languageCode]