我有這個字符串是查詢結果 07:40,09:00,10:20,11:40,| 08:00,09:00,|將字符串轉換爲數組2D中的java
1)我想消除最後一個|
2)如果你使用的是C將其轉換爲
String[][] matrix ={
{"07:40","08:00"},
{"09:00","09:00"},
{"10:20",null},
{"11:40",null}
};
我有這個字符串是查詢結果 07:40,09:00,10:20,11:40,| 08:00,09:00,|將字符串轉換爲數組2D中的java
1)我想消除最後一個|
2)如果你使用的是C將其轉換爲
String[][] matrix ={
{"07:40","08:00"},
{"09:00","09:00"},
{"10:20",null},
{"11:40",null}
};
喜嘗試此解決方案它的工作原理,而不是隻在一個完美的方式,但它照顧空串和格式不正確的琴絃享受
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;
}
這裏是主要的測試方法
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("");
}
}
}
++,那麼你可以試試這個:
#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;
}
我想:
1)elimitate最後 「|」使用例如substring()
2)分割字符串與string.split("|")
,並保持長度爲numTimes
3)在所述分割結果週期和由substr.split(",")
4分割每個子串)保持的長度的最大長度在一個int的分割稱爲len
5)創建結果陣列String[][] matrix = new String[len][numTimes]
5)創建一個用於在循環中循環for (int i = 0; i < len; i++){...
6)添加正確的值到matrix
(檢查null)
Upvoted解釋過程的必要步驟如此簡潔 – newcoder
試試這個:
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]));
}
}
我不要認爲把所有OP的代碼寫出來是OP學習的好方法:/ –
不,我同意你@CyberneticTwerkGuruOrc但是我希望他會熟悉JAVA中的API並且在下次他們學習時 – SMA
多謝ima初學者在java –
哪種編程語言? – SMA
這是固定格式,即由管隔開的列,即「|」嗎? – SMA
no是查詢結果中的變量Strin,是我通過管道分隔列 –