|
查看: 1195|回复: 9
|
J2ME Split and Check String 问题!!!
[复制链接]
|
|
|
用以下的Code成功split & display 12 14 13。但我要如何做检查
String
String test = "#12#14#13";
String[] result = split(test, "#");
for (int i=0; i<result.length; i++) {
Form.append(result[i]);
} |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 12-1-2010 03:32 PM
|
显示全部楼层
不好意思,还未写玩就不小心按了Enter。我要如何检查String test只可以输入12而已。谢谢
String mm = "12";
String test = "#12#14#13"; //User输入的
String[] result = split(test, "#");
for (int i=0; i<result.length; i++) {
Form.append(result[i]);
} |
|
|
|
|
|
|
|
|
|
|
发表于 13-1-2010 09:30 PM
|
显示全部楼层
|
用 TRY ... CATCH EXCEPTION .. |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 14-1-2010 01:48 PM
|
显示全部楼层
谢谢你的回复!我还是做不到, 你可以给我个例子吗?
其实我是想Check User Input,例如#12#14#13.当user input string 里除了String mm = "12"以外还包含其他数字, 就assign variable = false。 反之,,user input string 例如#12#12 就assign variable = true.谢谢! |
|
|
|
|
|
|
|
|
|
|
发表于 14-1-2010 04:36 PM
|
显示全部楼层
谢谢你的回复!我还是做不到, 你可以给我个例子吗?
其实我是想Check User Input,例如#12#14#13.当user ...
mikeng 发表于 14-1-2010 01:48 PM 
- String mm = "12";
- String test = "#12#14#13"; //User输入的
- String[] result = split(test, "#");
- for (int i=0; i<result.length; i++) {
- if ( result[i] == '12')
- System.out.println('is 12');
- }
复制代码
java我 不会。。。那我就假死下。。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 14-1-2010 06:00 PM
|
显示全部楼层
String mm = "12";
String test = "#12#14#13"; //User输入的
boolean check=false; //---侦查别的数字
String[] result = split(test, "#");
for (int i=0; i<result.length; i++) {
if(Integer.parseInt(result[i])!=12)
check=true;
}
if(check)
System.out.println("别的数字存在");
else
System.out.println("只有12存在"); |
|
|
|
|
|
|
|
|
|
|
发表于 14-1-2010 07:41 PM
|
显示全部楼层
本帖最后由 onlylonly 于 14-1-2010 07:46 PM 编辑
不是很建议单独使用 parseInt, 若要 parseInt, 那么最好加入 exception handling。 因为 string 里未必只是 Int 罢了。
直接 compare string 也可以, 拿了boonboon80 大大的 code 做了些许更改
- for ( String str : result)
- {
- str.trim();
- if( str.equals("12") == false )
- check = true;
- }
复制代码 以上是 J2SE 的, J2ME 不懂是否适用。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 14-1-2010 09:20 PM
|
显示全部楼层
用Split 把User输入的String test "#12#14#13" 的划分结果确定是result[1] = 12 ; result[2] = 14 ; result[3] = 13; 但我要如何检查当string 里不包含12或13就 assign variable boolean check = false?我还是做不到,希望你门帮帮忙,以下是我的Code:
当我把String test 改成#12#13, check还是false。。。
boolean check = false;
String test = "#12#14#13";
String[] result = split(test, "#");
for (int i=0; i<result.length; i++){
if(result[i].equals("12") || result[i].equals("13") ){
check = true;
}else{
check = false;
break;
}
}
if(check == true){
Form.append("true");
}else{
Form.append("false");
} |
|
|
|
|
|
|
|
|
|
|
发表于 14-1-2010 10:22 PM
|
显示全部楼层
本帖最后由 boonboon80 于 14-1-2010 10:23 PM 编辑
回复 8# mikeng
test split 后,result[0]可能是empty string,所以跑入check = false 了。
boolean check = false;
String test = "#12#14#13";
String[] result = split(test, "#");
for (int i=0; i<result.length; i++){
//-------检查empty string-------------
if(result.equals("") || result==null)
continue;
result.trim();
if(result.equals("12") || result.equals("13") ){
check = true;
}else{
check = false;
break;
}
}
if(check == true){
Form.append("true");
}else{
Form.append("false");
}
回:onlylonly
onlyonly大大的例子是对的。由于对比数字,我通常都喜欢把它转成int来检查,以确定内容是数字和速度会快点。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 15-1-2010 10:11 AM
|
显示全部楼层
|
谢谢各位大哥帮忙,我做到了。往后还有很多地方需大家指点的。谢谢! |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|