请问一个截取字段的问题 比如 String a = "5分钟";这个5分钟有可能是10分钟,或者20分钟这种不确定的数。如何只截取数字部分呢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String str = "35分钟";int index = str.indexOf("分钟");String result = "0";if( index > 0 ){ result = str.subString(0,index);}System.out.println(result); String[] strArray = str.split("分钟");or String strOutput = str.replaceAll("分钟", ""); "分钟"2个字的长度是固定的,所以....a.substring(0, a.length()-2) 这样 public static void main(String[] args) { String str = "拉拉5分钟呵呵10分钟"; String regex = "[\\d]+分钟"; Pattern pattern = Pattern.compile(regex); Matcher m = pattern.matcher(str); while(m.find()) { String matched = m.group(); System.out.println(matched.substring(0,matched.indexOf("分钟"))); } } 用正则表达式吧:import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegTest{ public static void main(String[] args){ Pattern p = Pattern.compile("[0-9]+"); Matcher m = p.matcher("10分钟"); if(m.find()){ System.out.println(m.group()); } }} public static void main(String args[]) { String str = "*分钟"; String target= str.replaceAll("分钟", ""); //System.out.println(target); } public class test{ public static void main(String args[]) { String str="523242分zhonga"; byte b[] = str.getBytes(); for(int i=0;i<b.length;i++) { System.out.println(""+i); System.out.println(b[i]); if(Character.isDigit(b[i])) continue; else b[i]=' '; } String str2= new String(b); str2=str2.trim(); System.out.println(str2); }} http://bbs.tarena.com.cn/去这里看看 向数据库添加客户信息SQL语句出错 求教 关于combo中的获得数据 重写JFrame的最大化及恢复事件? 如何正确显示文件中读出的Unicode转义码 求一份俄罗斯方块的原码示例 请问哪儿有《最新 JAVA 2 核心技术》 卷I 和卷II 下载?? java 对oracle 表监控问题 jboss能够设置虚拟目录吗 关于awt的一个简单问题。 求解为什么运行不出来啊。本人是新手啊 如何把一个PDF文件变成序列化对象? 清理关键词 finalize
String str = "35分钟";
int index = str.indexOf("分钟");
String result = "0";
if( index > 0 )
{
result = str.subString(0,index);
}System.out.println(result);
or
String strOutput = str.replaceAll("分钟", "");
String str = "拉拉5分钟呵呵10分钟";
String regex = "[\\d]+分钟";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(str);
while(m.find()) {
String matched = m.group();
System.out.println(matched.substring(0,matched.indexOf("分钟")));
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class RegTest
{
public static void main(String[] args){
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher("10分钟");
if(m.find()){
System.out.println(m.group());
}
}
}
public static void main(String args[]) {
String str = "*分钟";
String target= str.replaceAll("分钟", "");
//System.out.println(target);
}
{
public static void main(String args[])
{
String str="523242分zhonga";
byte b[] = str.getBytes();
for(int i=0;i<b.length;i++)
{
System.out.println(""+i);
System.out.println(b[i]);
if(Character.isDigit(b[i]))
continue;
else b[i]=' ';
}
String str2= new String(b);
str2=str2.trim();
System.out.println(str2);
}
}