Reputation: 79
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}
};
Upvotes: 0
Views: 112
Reputation: 1828
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("");
}
}
}
Upvotes: 0
Reputation: 37023
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]));
}
}
Upvotes: 1
Reputation: 11712
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)
Upvotes: 3
Reputation: 3800
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;
}
Upvotes: 1