0

一个名为 item_list 的 MySQL 表有一个名为 description 的字段,问题是以前的程序员将项目的名称和描述组合在一个名为 description 的字段中。数据现在为 20k+。现在我在迁移过程中会遇到问题。那么我该如何分离一个

String description="BEARING, ROLLER 23230CKE4 SPHERICAL"  

成两个新字符串

String name="BEARING" 
String description="ROLLER 23230CKE4 SPHERICAL"

任何帮助将不胜感激。

4

4 回答 4

0

你可以试试这种方式

String description="BEARING, ROLLER 23230CKE4 SPHERICAL";
String [] arr=description.split(",");
System.out.println(arr[0]);
System.out.println(arr[1]);

输出

BEARING
ROLLER 23230CKE4 SPHERICAL

String Split 方法返回一个字符串数组。由于 String 描述中有一个逗号(,),所以整个字符串将被拆分为 2 个字符串。

于 2013-09-23T07:53:03.973 回答
0

您可以使用 StringTokenizer

像这样的东西

 StringTokenizer st = new StringTokenizer(description,",");
 String name=st.nextToken();
 description=st.nextToken();
于 2013-09-23T08:08:48.933 回答
0

,不幸的是,如果组合字符串中的字符串超过 1 个,则字符串拆分函数将无法正常工作。

我建议你只拆分第一个,

int idx = description.indexOf(',');
if (idx != -1) { // if there is a comma
    name = description.substring(0, idx);
    description = description.substring(idx+1);
} else {
    ???? // no comma in description
}
于 2013-09-23T09:18:36.523 回答
0

所有答案的组合..,解决问题。

String  name="",new_d ="";
String description="BEARING, ROLLER 23230CKE4 SPHERICAL";

int idx = description.indexOf(',');
if (idx != -1) { // if there is comma
String arr[]=description.split(",\\s*");
name=arr[0].toString();
new_d=arr[1].toString();
} 
else {
            // if there is no comma
name=description;
new_d="";
}
System.out.println(name);
System.out.println(new_d);
于 2013-09-24T05:54:02.523 回答