关于字符串拆分的问题

Candylibin 2012-04-25 10:23:38
字符串拆分: 从左到右有五个字段:

(字段名) name targetname params type mark(当前时间)

(字段值) SYS_DOC_LIST_1_2 1/2/1_2.json 1,2 sys_doclist now()


现在只有一个参数:1,2(是name的) 拆分成上述参数 做数据库插入操作 表名叫:staticfile_list

...全文
160 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
qybao 2012-04-25
  • 打赏
  • 举报
回复
have a try
String s = (String) this.getParameter("name");
//s = "1,2";
String[] n = s.split(",");
StringBuilder _str = new StringBuilder(n[0]);
StringBuilder sstr = new StringBuilder(n[0]);
for (int i=1; i<n.length; i++) {
_str.append("_").append(n[i]);
sstr.append("/").append(n[i]);
}
String value = String.format("'SYS_DOC_LIST_%s', '%s/%s.json', '%s', 'sys_doclist', now()",
_str, sstr, _str, s);
String sql = String.format("insert into staticfile_list(name,targetname,params,mark) values (%s)", value);
System.out.println(sql);
安特矮油 2012-04-25
  • 打赏
  • 举报
回复
你的value都循环拼接去了......
安特矮油 2012-04-25
  • 打赏
  • 举报
回复
觉得应该这样才对

String s = (String) this.getParameter("name");
//"1,2"
String value = "";
String[] n = s.split("_");
String name = "SYS_DOC_LIST_";
String targetname = "1/2/";
String params = "";
String make = "now()";
for (int i = 0; i < n.length; i++) {
name += n[i];
targetname += n[i];
targetname += n[i];
// value += "('SYS_DOC_LIST_" + n[i] + "','1/2/" + n[i] + ".json','"
// + n[i] + "',now())_";
}
value = "('" + name + "','" + targetname + ".json','" + params + "','" + make + ")";
//value = value.substring(0, value.length() - 1);
Candylibin 2012-04-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

你是要直接生成sql是吧?但是你传入的参数是1,2,那么
String value = "";
String [] n = s.split("_");
这里得到的应该是{"1,2"}才对撒
[/Quote]
对我用逗号拆就出来了 变成:SYS_DOC_LIST_1 SYS_DOC_LIST_2了,


,但是现在还是插不进去
安特矮油 2012-04-25
  • 打赏
  • 举报
回复
你是要直接生成sql是吧?但是你传入的参数是1,2,那么
String value = "";
String [] n = s.split("_");
这里得到的应该是{"1,2"}才对撒
Candylibin 2012-04-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

是要这样吗?
Java code

public static void main(String[] args) {
String a= "SYS_DOC_LIST_1_2 1/2/1_2.json 1,2 sys_doclist now()";
String[] strs = a.split("\\s+");
for(String s : strs){
……
[/Quote]

我给你看看我的程序,你改改

程序:

public class AddStaticfile_listOperation extends Operation{

public Result execute() throws OpertionException {




String s = (String)this.getParameter("name");
String value = "";
String [] n = s.split("_");

for(int i = 0;i<n.length;i++){
value+="('SYS_DOC_LIST_"+n[i]+"','1/2/"+n[i]+".json','"+n[i]+"',now())_";
}
value = value.substring(0, value.length()-1);

String sql = "insert into staticfile_list(name,targetname,params,mark)values"+value;

int a = this.getDataBaseProvider().executeUpdate(sql, null);

if(a>0){
return new AddStaticfile_listSuccessful();
}else{
return new AddStaticfile_listFail();
}

}

}

测试:

public class AddStaticfile_listTest extends BreadthTestCase {
public void testAddStaticfile_listSuccessful() {

Result rs = null;

String value = "1,2";

OperationWorker worker = new OperationWorker(AddStaticfile_listOperation.class);
worker.addParameter("name", value);


try {
rs = worker.execute();
} catch (OpertionException e) {
e.printStackTrace();
}
assertTrue(rs instanceof AddStaticfile_listSuccessful);
}
}


安特矮油 2012-04-25
  • 打赏
  • 举报
回复
是要这样吗?

public static void main(String[] args) {
String a= "SYS_DOC_LIST_1_2 1/2/1_2.json 1,2 sys_doclist now()";
String[] strs = a.split("\\s+");
for(String s : strs){
System.out.println(s);
}
}
安特矮油 2012-04-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 3 楼 的回复:

SYS_DOC_LIST_1_2 --name ??
1/2/1_2.json --targetname
1,2 -- params ? name ?
sys_doclist --type ??
now() --mark??

那你就通过空格split


对这个就是对着的,,我知道用split,我自己弄失败了
[/Quote]
是一一对应?
1,2对应的params还是name啊?
Candylibin 2012-04-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

SYS_DOC_LIST_1_2 --name ??
1/2/1_2.json --targetname
1,2 -- params ? name ?
sys_doclist --type ??
now() --mark??

那你就通过空格split
[/Quote]

对这个就是对着的,,我知道用split,我自己弄失败了
五哥 2012-04-25
  • 打赏
  • 举报
回复
SYS_DOC_LIST_1_2 --name ??
1/2/1_2.json --targetname
1,2 -- params ? name ?
sys_doclist --type ??
now() --mark??

那你就通过空格split
Candylibin 2012-04-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

规则说明确一点,各个对应关系是什么,要得到什么样的东西
[/Quote]
规则没什么,这个targetname对应1/2/1_2.json

(数据库的结构)
安特矮油 2012-04-25
  • 打赏
  • 举报
回复
规则说明确一点,各个对应关系是什么,要得到什么样的东西
qybao 2012-04-25
  • 打赏
  • 举报
回复
format方法没有,那估计是1.5以前的jdk或j2me之类的了
自己用字符串拼接就可以了
String s = (String) this.getParameter("name");
//s = "1,2";
String[] n = s.split(",");
StringBuffer _str = new StringBuffer(n[0]);
StringBuffer sstr = new StringBuffer(n[0]);
for (int i=1; i<n.length; i++) {
_str.append("_").append(n[i]);
sstr.append("/").append(n[i]);
}
StrungBuffer value = new StringBuffer();
value.append("('SYS_DOC_LIST_").append(_str.toString()).append("', '");
value.append(sstr.toString()).append("/").append(_str.toString()).append(".json', '");
value.append(s).append("', 'sys_doclist', now())");
String sql = "insert into staticfile_list(name,targetname,params,mark) values " + value.toString();
System.out.println(sql);

qybao 2012-04-25
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
String.format (The method format(String, StringBuffer, StringBuffer, StringBuffer, String) is undefined for the
type String)
不成啊[/Quote]
什么版本的jdk?
你把用到StringBuilder的地方换成toString试试看,即
String value = String.format("'SYS_DOC_LIST_%s', '%s/%s.json', '%s', 'sys_doclist', now()",
_str.toString(), sstr.toString(), _str.toString(), s);
Candylibin 2012-04-25
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

觉得应该这样才对
Java code

String s = (String) this.getParameter("name");
//"1,2"
String value = "";
String[] n = s.split("_");
String name = "SYS_DOC_LIST_";
……
[/Quote] 1/1.json 1/1 要自动生成的好像,不能写死了
Candylibin 2012-04-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

have a try
Java code
String s = (String) this.getParameter("name");
//s = "1,2";
String[] n = s.split(",");
StringBuilder _str = new StringBuilder(n[0]);
StringBuilder sstr = new StringBuilder(n[0]……
[/Quote]
String.format (The method format(String, StringBuffer, StringBuffer, StringBuffer, String) is undefined for the
type String)
不成啊

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧