0

I have this String is a result from query 07:40,09:00,10:20,11:40,|08:00,09:00,|

1) i want to eliminate the last ,|

2) convert it to

String[][] matrix ={
    {"07:40","08:00"},
    {"09:00","09:00"},
    {"10:20",null},
    {"11:40",null}
};
4
  • 2
    Which programming language? Commented Dec 26, 2014 at 16:57
  • Is that fixed format i.e. columns seperated by pipe i.e. "|"? Commented Dec 26, 2014 at 17:09
  • no is a variable Strin from a result of Query, yes i separat the column by pipe Commented Dec 26, 2014 at 17:11
  • Will it always be 6 variables? Does order matter? Will the pipeline characters be fixed? There isn't enough info here to help Commented Dec 26, 2014 at 17:28

4 Answers 4

3

I would:

1) elimitate the last ",|" using e.g. substring()

2) split the string with string.split("|"), and keep the length as numTimes

3) cycle over the split result and split each substring by substr.split(",")

4) keep the maximum length of the length of the splits in an int called len

5) create the result array String[][] matrix = new String[len][numTimes]

5) create a for loop for (int i = 0; i < len; i++){...

6) within the loop add the correct values into matrix (check for null)

Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted for explaining the necessary steps of the process so succinctly
1

If you are using C++ then you can try this :

#include <bits/stdc++.h>
using namespace std;

vector<string>split(string str,string Separator)
{
    vector<string>answer;string temp;
    int len=str.size();
    for(int i=0;i<len;i++)
    {
        bool isSeparator=false;
        for(int j=0;j<Separator.length();j++)
          if(str[i]==Separator[j])
            isSeparator=true;
        if(!isSeparator)
        {
            temp+=str[i];continue;
        }
        if(temp!="")
          answer.push_back(temp);temp="";
    }
    if(temp!="")
      answer.push_back(temp);
    return answer;
}
int main()
{
    int i,j;
    string str="07:40,09:00,10:20,11:40,|08:00,09:00,|";
    vector<string>v=split(str,"|"); // First split with respect to '|'
    vector<string>matrix[100]; // Maximum row of time
    for(i=0;i<v.size();i++)
    {
        vector<string>temp;
        temp=split(v[i],","); // Now split with respect to ','
        for(j=0;j<temp.size();j++)
        {
            matrix[j].push_back(temp[j]);
        }
    }
    for(i=0;;i++)
    {
        if(matrix[i].size()==0) // Break the loop, because no time will be on below
          break;
        for(j=0;j<matrix[i].size();j++)
          cout<<matrix[i][j]<<" ";
        cout<<"\n";
    }
return 0;
}

4 Comments

Thanks but OP needs answer for java.
@almasshaikh it was not stated at first :(
tanks but i m using java
@AliAkber I know and i did asked OP to add. Really appreciate you helping OP.
1

Try this:

public static void main(String ars[]) {
String string = "11:40,|08:00,09:00,|";
String[] str1 = string.split("\\|");
if (str1.length != 2) {
  throw new IllegalArgumentException("I dont see a seperator | in your String");
}
String[] rows = str1[0].split(",");
String[] cols = str1[1].split(",");
int maxLength = rows.length > cols.length ? rows.length : cols.length;
String matrix[][] = new String[maxLength][2];

for (int row=0; row<rows.length; row++) {
  matrix[row][0] = rows[row];
}
for (int col=0; col<cols.length; col++) {
  matrix[col][1] = cols[col];
}

for (int i=0; i<maxLength; i++) {
  System.out.println(Arrays.toString(matrix[i]));
}
}

4 Comments

I don't think writing all of OP's code out is a good way for OP to learn :/
No i agree with you @CyberneticTwerkGuruOrc. But am hoping he is going to get familiar with API's in JAVA and learn them so next time he is going to use those.
but if i have String string = "11:40,|08:00,09:00,|08:00,09:00"; it should be dynamic
@almasshaikh help me plz
0

hi try this solution it works and not only in a perfect way but it take care of null strings ans malformed strings enjoy

             public static String[][] getModel(String answer){
     try {
         System.out.println(answer);
             if(answer.contains("|")){
                            String[] cols=answer.split(",\\|");

                            System.out.println("case found");
                            System.out.println("size 1:"+cols.length);

                            int dimensionCol=cols.length;
                            int dimensionRow=cols[0].split(",").length;

                            System.out.println(dimensionCol+" "+dimensionRow);
                            String[][] result=new String[dimensionRow][dimensionCol];

                            int i=0,j=0;
                            for(String colElement:cols){
                                i=0;
                                String[] datas = colElement.split(",");
                                for (String data : datas) {
                                    result[i][j]=(!data.equals(""))?data:null;
                                    System.out.print(result[i][j]);
                                    i++;
                                }
                                System.out.println("");
                                j++;
                                         }
                            return result;
                           }else{


                                  System.out.println("empty String return by null");
                                  return null;


                           }
          }catch(Exception e){e.printStackTrace();}
     return null;
   }

here is the main test methode

   public static void main(String[] args) {


   // String model = "07:40,09:00,10:20,11:40,|08:00,09:00,|";
    String model = "";
    String[][] model1 = getModel(model);
          if (model1!=null) {
        for (String[] model11 : model1) {
              for (String model111 : model11) {
                  System.out.print(model111+" ");
              }
              System.out.println("");

    }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.