查找字符串中的数字

时间:2023-01-03 21:35:05

查找字符串中的数字

String numStr = "0123456789";
int index = 0;
for(int i=0;i<numStr.length();i++){
while((index = str.indexOf(numStr.charAt(i), index)) != numStr.length()){
if(index == -1)break;
result[str.length()-index-1] = String.valueOf(str.charAt(index));
++index;
}
}
更改的数字转中文大写代码

package zeng.test;

public class Num2CnNum {

private static final String cnNum1[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"} ;
private static final String cnNum2[] = {"个","拾","佰","仟","万","亿"};
private static final String numStr = "0123456789";

public static void main(String[] arg0){
new Num2CnNum();
}

public Num2CnNum(){

System.out.println(this.toCnNumString("100000080.12345"));
System.out.println(this.toCnNumString(".12345"));
System.out.println(this.toCnNumString("100.1045"));
}

private String toCnNumString(String str){


//判断是否为存在小数点
if(str.contains(".")){
String splitNum[] = str.split("\\.");

StringBuffer sb = new StringBuffer();
for(int i=0;i<splitNum[1].length();i++){
int num = Integer.parseInt(String.valueOf(splitNum[1].charAt(i)));
sb.append(cnNum1[num]);
}
if(splitNum[0].length() != 0){
return toCnNum(splitNum[0])+"点"+sb.toString();
}else{
return cnNum1[0]+"点"+sb.toString();
}

}else{
return toCnNum(str);
}
}

private String toCnNum(String str){
int index = 0;
String result[]=new String[str.length()];
for(int i=0;i<numStr.length();i++){
while((index = str.indexOf(numStr.charAt(i), index)) != numStr.length()){
if(index == -1)break;
result[str.length()-index-1] = String.valueOf(str.charAt(index));
++index;
}
}
StringBuffer sb = new StringBuffer();
for(int i=str.length()-1;i>=0;i--){
int num = Integer.parseInt(result[i]);
//sb.append(cnNum1[num]+cnNum2[i]);
if(i<=4){
sb.append(cnNum1[num]+cnNum2[i]);
}else if(i>=5 && i<=7){
sb.append(cnNum1[num]+cnNum2[i-4]);
}else if(i == 8 ){
sb.append(cnNum1[num]+cnNum2[5]);
}
}

return sb.toString().replace("个", "");
}


}