如何在#*#*上分割字符串

时间:2019-02-27 06:41:12

标签: java android split

我有一个String,其值如下:

B/W 0-2 hours of bus start time#*#*B/W 2-9 hours of bus start time#*#*Above 9 hours of bus start time--0%#*#*50%#*#*90%

您如何在split()#*#*?我需要如下输出:

B/W 0-2 hours of bus start time

B/W 2-9 hours of bus start time

Above 9 hours of bus start time

0%

50%

90%

5 个答案:

答案 0 :(得分:2)

这样分割字符串:

  String mStr = "B/W 0-2 hours of bus start time#*#B/W 2-9 hours of bus start time##Above 9 hours of bus start time--0%##50%##*90%";
        Log.e("mStr==", "===" + mStr);

        String[] pickList = mStr.split("#\\*#|##|--|##\\*|\\*");

        Log.e("pickList==", "===" + pickList.length);

        for (int i = 0; i < pickList.length; i++) {
            Log.e("pickList==", "===" + pickList[i]);
        }

选择列表内部,您将获得包含拆分数据的输出列表。

答案 1 :(得分:0)

尝试以下-

ct.Counter(words)

答案 2 :(得分:0)

您的字符串应包含相同的定界符。如果您有不同的定界符,则需要预测可以使用哪些定界符,并将这些定界符作为参数传递给由|分隔的split()。签名。

例如,要将#和*符号用作分隔符,则需要这样编写: stringvariable.split(“ \#| \ *”);

答案 3 :(得分:0)

您可以使用单个alternation来匹配1个重复的import pandas as pd def select_real_product(string_to_elaborate): return string_to_elaborate.split('+')[0].strip() df = pd.read_csv("Remarks_Drug.csv", delimiter=',', names=("product", "filename")) df['product'] = df['product'].apply(select_real_product) df.to_csv("drug_output100.csv", sep=',', na_rep='empty',index_label=False, index=False) 或匹配#*

--

查看regex demo | Java demo

例如:

(?:#\*)+|--

结果

String s = "B/W 0-2 hours of bus start time#*#*B/W 2-9 hours of bus start time#*#*Above 9 hours of bus start time--0%#*#*50%#*#*90%";
String[] parts = s.split("(?:#\\*)+|--");

for (String part : parts) {
    System.out.println(part);
}

答案 4 :(得分:0)

String finalValue =“”; String mystring =“ B / W公交开始时间0-2小时#*# B / W公交开始时间2-9小时#公交开始时间9小时以上- 0%# 50%##* 90%“;

String[] splitSt1 = mystring.split("--");
String detailsString = splitSt1[0];
String percentageString = splitSt1[1];
String[] detailsSt1 = detailsString.split("#\\*#\\*");
String[] percentageSt2 = percentageString.split("#\\*#\\*");

if(detailsSt1.length == detailsSt1.length ){
    for(int i=0;i<=(detailsSt1.length-1);i++ )
    {
        finalValue = finalValue +"* "+ detailsSt1[i]+ "-->" + percentageSt2 [i] +"\n"+"\n";
        my_text_view_id.setText(finalValue);
    }

}

输出:

  • 黑白0-2小时的巴士启动时间-> 0%
  • 黑白2-9小时的巴士启动时间-> 50%
  • 巴士启动时间超过9小时-> 90%