黑马程序员技术交流社区
标题:
输入一个字符串,内有数字和非数字字符?求大大神?
[打印本页]
作者:
simonqian
时间:
2013-5-12 21:47
标题:
输入一个字符串,内有数字和非数字字符?求大大神?
本帖最后由 simonqian 于 2013-5-13 08:34 编辑
列入:a123x456 17960?302t5876将其中连续的数字作为一个整数,依次存放到一数组a中。例如,123放在a[0],456放在a[1]。。。。。。。。统计共有多少个整数,并输出这些数?
作者:
乎¤_¤乎
时间:
2013-5-12 22:51
抛砖引玉,共同提高
//用正则表达式把非数字剔除
class stringdemo
{
public static void main(String[] args)
{
String str="nn34324huiiu23424hui123as23";
String[]arr=retArray(str);
int[]a=newArray(arr);
System.out.println("总共有"+a.length+"个整数");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
//得到字符串数组
public static String[] retArray(String str)
{
//用正则表达式把非数字剔除
String[]buf=str.split("\\D+");
//考虑到如果第一位是非数字,那么切出来的第一位是空字符串
if(buf[0].equals(""))
{
String[]buf1=new String[buf.length-1];
for(int i=0;i<buf.length-1;i++)
{
buf1[i]=buf[i+1];
}
return buf1;
}
return buf;
}
//将字符串数组转换为整数数组
public static int[] newArray(String[] arr)
{
int[]arr1=new int[arr.length];
for(int i=0;i<arr.length;i++)
{
arr1[i]=Integer.parseInt(arr[i]);
}
return arr1;
}
}
复制代码
作者:
孙金鑫
时间:
2013-5-12 23:08
import java.util.regex.*;
import java.util.*;
class RegxTest
{
public static void main(String[] args)
{
String str = "a123x456 17960?302t5876";
String reg = "\\d*";
int num;
TreeSet<String> ts = new TreeSet<String>();
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while(m.find())
{
ts.add(m.group());
}
num = ts.size();
for(String s : ts)
{
if(s.equals(""))
{
num--;
continue;
}
System.out.print(s+" ");
}
System.out.println("长度为:"+num);
}
}
复制代码
作者:
许智敏
时间:
2013-5-13 16:26
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class 取出数字 {
public static void main(String[] args){
String s = "a123x456 17960?302t5876";
getNumber(s);
}
public static void getNumber(String s){
ArrayList<Integer> al = new ArrayList<Integer>();//定义集合,将符合正则的填入该集合中
String regex = "\\d+";//定义正则,有一个或多个数字则符合规则
Pattern p = Pattern.compile(regex);//将正则封装成对象。
Matcher m = p.matcher(s);//通过正则对象获取匹配器对象。
while(m.find())//直到没有符合要求的串时,结束while
al.add(Integer.parseInt(m.group()));//用group返回匹配的子序列,为String型,
//Integer.parseInt(string)转成int型,自动装箱,添加到al集合中。
System.out.print("总共有"+al.size()+"个数,这些数字为:"+al);//输出
}
}
复制代码
作者:
许智敏
时间:
2013-5-13 16:47
//忘了加到数组a里了,修改一下。
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class 取出数字 {
public static void main(String[] args){
String s = "a123x456 17960?302t5876";
getNumber(s);
}
public static void getNumber(String s){
ArrayList<Integer> al = new ArrayList<Integer>();//定义集合,将符合正则的填入该集合中
String regex = "\\d+";//定义正则,有一个或多个数字则符合规则
Pattern p = Pattern.compile(regex);//将正则封装成对象。
Matcher m = p.matcher(s);//通过正则对象获取匹配器对象。
while(m.find())//直到没有符合要求的串时,结束while
al.add(Integer.parseInt(m.group()));//用group返回匹配的子序列,为String型,Integer.parseInt(string)转成int型,自动装箱,添加到al集合中。
Object[] a = al.toArray();//将集合变为数组
System.out.print("总共有"+a.length+"个数,这些数字为:");//输出
for(Object x : a)
System.out.print(x+" ");//x默认调用toString();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2