void displayResult() {
String str = "tamilnadu||chennai-karanataka||bengaluru";
String[] res = str.split("\\-");
System.out.println(res.length);//res length is 2
//res contains two strings splited by -
String[] result = res.toString().split("\\||");
//again splitting by || but getting as characters, i need to get word by word
// how to achieve this
System.out.println(result.length);//result length is 28
}
// i was supposed to get tamilnadu and chennai from first string[] res
String [] res包含两个由-分割的字符串-我正尝试以相同的方式分割res以使字符串由||分割管道符号,但我越来越像字符 怎样像以前一样
答案 0 :(得分:1)
您需要使用两个\
来转义|
,因此正则表达式为\\|\\||-
,我们只能使用一个split
String str = "tamilnadu||chennai-karanataka||bengaluru";
String[] strs = str.split("\\|\\||-");
for(String s:strs){
System.out.println(s);
}
输出为
tamilnadu
chennai
karanataka
bengaluru
答案 1 :(得分:1)
您不应致电res.toString
。它将返回如下内容:
[Ljava.lang.String;@2c7b84de
相反,您可以使用正则表达式\|\|
拆分res中的元素:
String[] result = res[0].split("\\|\\|");
System.out.println(result.length); // 2
答案 2 :(得分:1)
我认为您必须尝试学习Regex。您可以使用|
运算符来指定OR
条件。因此,如果您使用以下命令,则应该获得所需的数组:
String [] res = str.split(“ \-| \ | \ |”);
即您的方法将是:
void displayResult(){
String str = "tamilnadu||chennai-karanataka||bengaluru";
String[] res = str.split("\\-|\\|\\|");
System.out.println(Arrays.asList(res)); // don't forget to import Arrays
}
答案 3 :(得分:1)
不要两次使用拆分方法。您可以使用一种拆分方法来完成任务。像这样
void displayResult() {
String str = "tamilnadu||chennai-karanataka||bengaluru";
String[] res = str.split("\\|\\||-");
for(String city : res){
System.out.println(city);
}
}
您的输出将是:-
泰米尔纳德邦
金奈
karanataka
班加罗尔
答案 4 :(得分:0)
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "POST", MyURL, False
MyRequest.setRequestHeader "Content-Type", "application/json"
MyRequest.send ("{""ProductRollupId"":<value from excel cell>,
""ProductPartnerCode"":""<value from excel cell>""}")