在括号内用小时分割一个字符串

时间:2016-09-12 10:10:06

标签: java android string

这是我从服务器获取的数据。我想获取小时并将它们存储在一个字符串中

(09:00-12:00) (17:00-20:30)
(12:00-16:00) (20:00-22:00)
(11:00-15:00) (19:00-21:30)
(07:00-11:00) (16:00-20:00)
(10:00-14:00) (18:30-21:00)
(05:00-09:00) (14:00-17:00)
(06:00-10:00) (15:00-19:00)

所以这就是我想要的结果

String A = 09:00
String B = 12:00
String C = 17:00
String D = 20:30
...ans so on until

String A2 = 19:00

我对代码的尝试基本没用,所以请你帮我解决这个问题吗?

到目前为止,我所能做的只是

public class Main
{
    public static void main(String[] args)
    {

        String data ="(09:00-12:00) (17:00-20:30)" + '\n' +
            "(12:00-16:00) (20:00-22:00)" + '\n' +
            "(11:00-15:00) (19:00-21:30)" + '\n' +
            "(07:00-11:00) (16:00-20:00)" + '\n' +
            "(10:00-14:00) (18:30-21:00)" + '\n' +
            "(05:00-09:00) (14:00-17:00)" + '\n' +
            "(06:00-10:00) (15:00-19:00)";

//          String regex ="\\(|\\)";
//          data= data.replaceAll(regex,"");
//          

        String[] splited_hyphen_1,splited_hyphen_2;

        for (int i=0;i < data.length();i++)
        {
            //Get the string from json
            //JSONObject c = jsonArray.getJSONObject(i);
            String str= data;

            //split the string with space
            String[] splited_space = str.split("\\s+");

            //split the first string with hypen
            splited_hyphen_1 = splited_space[0].split("-");

            //split the first string with hypen
            splited_hyphen_2 = splited_space[1].split("-");

            for (int j=0;j < splited_hyphen_1.length;j++)
            {
                if (splited_hyphen_1[j].contains("("))
                    splited_hyphen_1[j] =  splited_hyphen_1[j].replace("(","");

                if (splited_hyphen_1[j].contains(")"))
                    splited_hyphen_1[j] =  splited_hyphen_1[j].replace(")", "");
                //System.out.println(Arrays.asList(splited_hyphen_1));
            }


            for (int j=0;j < splited_hyphen_2.length;j++)
            {
                if (splited_hyphen_2[j].contains("("))
                    splited_hyphen_2[j] =  splited_hyphen_1[j].replace('(', ' ');

                if (splited_hyphen_2[j].contains(")"))
                    splited_hyphen_2[j] =  splited_hyphen_1[j].replace(')', ' ');


                //System.out.println(splited_hyphen_2[j]);


            }


        }




    }
}

2 个答案:

答案 0 :(得分:1)

使用此分割(“ - ”) 例如:

Job Seeker (Candidate looking for new job)

Employees ( who post a job and hiring job seeker candidate)

Premium Employees ( who post a job and hiring job seeker candidate and have some more functionality then Employees )

Guest ( who can only see all the job seeker , Employees , Preminum Employees post )

答案 1 :(得分:1)

尝试:

   String[] splited_hyphen_1,splited_hyphen_2;

    for(i=0;i<jsonArray.length();i++)
    {
       //Get the string from json
       JSONObject c = jsonArray.getJSONObject(i);
       String str=c.getString("key_value");

       //split the string with space
       String[] splited_space = str.split("\\s+");

        //split the first string with hypen
        splited_hyphen_1= splited_space[0].split("-");

        //split the first string with hypen
        splited_hyphen_2= splited_space[1].split("-");

       for(int j=0;j<splited_hyphen_1.length;j++)
       {
         if(splited_hyphen_1[j].contains('('))
         splited_hyphen_1[j]=  splited_hyphen_1[j].replace('(',' ');

         if(splited_hyphen_1[j].contains(')'))
         splited_hyphen_1[j]=  splited_hyphen_1[j].replace(')',' ');

       }


        for(int j=0;j<splited_hyphen_2.length;j++)
       {
         if(splited_hyphen_2[j].contains('('))
         splited_hyphen_2[j]=  splited_hyphen_1[j].replace('(',' ');

         if(splited_hyphen_2[j].contains(')'))
         splited_hyphen_2[j]=  splited_hyphen_1[j].replace(')',' ');

       }

    }

现在打印数组splited_hyphen_1,splited_hyphen_2;