怎么将一串数字拆分成单个数字?

LazyCat2222 2009-06-15 10:45:53
我从文本框获得一串输入的数字,例如: "12345678"
怎么将它拆分成单个单个的数字保存起来呢?
...全文
6029 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
两种方法。呵呵,一种使用 toCharArray,另一种使用取模一个数字一个数字取。

public class Test5 {

public static void main(String[] args) {
String[] strs = {
"12347890", "00012350", "00000", "00001000"
};

System.out.println("convert1:");
for(int i = 0; i < strs.length; i++) {
int[] nums = convert1(strs[i]);
System.out.printf(" %-15s %s%n", strs[i], Arrays.toString(nums));
}

System.out.println("\nconvert2:");
for(int i = 0; i < strs.length; i++) {
int[] nums = convert2(strs[i]);
System.out.printf(" %-15s %s%n", strs[i], Arrays.toString(nums));
}
}

public static int[] convert1(String strNum) {
String str = preProcess(strNum);
int[] nums = new int[str.length()];
for(int k = 1, num = Integer.parseInt(str); num > 0; k++, num /= 10) {
nums[nums.length - k] = num % 10;
}
return nums;
}

public static int[] convert2(String strNum) {
String str = preProcess(strNum);
int[] nums = new int[str.length()];
char[] chs = str.toCharArray();
for(int i = 0; i < chs.length; i++) {
nums[i] = chs[i] - '0';
}
return nums;
}

private static String preProcess(String strNum) {
if(strNum == null || strNum.length() == 0 || !strNum.matches("\\d+")) {
throw new IllegalArgumentException("number format error!");
}
// 去掉前面的 0
return strNum.replaceAll("^0+(\\d+)", "$1");
}
}
xfzhuhan 2009-06-15
  • 打赏
  • 举报
回复
String  str = "abcdefg";
char temp [] = new char[str.length()];
for(int i=0;i<str.length();i++){
temp[i] = str.charAt(i);
System.out.println(temp[i]);
}
xnjnmn 2009-06-15
  • 打赏
  • 举报
回复

public class TestString {
public static char[] getString(String s){

return s.toCharArray();
}
public static void main(String[] args) {
char[] cs= getString("12345678");
for (int i = 0; i < cs.length; i++) {
System.out.println(cs[i]);
}

}
}





<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript"><!--
function splitString(){
var name = document.getElementById("name").value;
var nameArray=new Array([name.length]);
for(var i = 0;i<name.length;i++){
nameArray[i]=name.charAt(i);
//此打印出来
alert(nameArray[i]);
//return nameArray;
}
}

</script>
</head>

<body>
<input type="text" id="name" value="12dfdf5678"/>
<br>
<input type="button" onclick="splitString()" value="test"/>
</body>
</html>
LazyCat2222 2009-06-15
  • 打赏
  • 举报
回复
对哦,一串数字,获得也就是一串字符串了。就跟拆字符串一样的。
谢谢各位了 懂了!
nnb200890 2009-06-15
  • 打赏
  • 举报
回复
up!
  • 打赏
  • 举报
回复

public static void main(String[] args) throws Exception
{
String a = "1234567";
char b[] = a.toCharArray();
for(int i=0;i<b.length;i++)
{
String c = b[i]+"";
int d = Integer.parseInt(c);
System.out.println(d);
}
}
baishengjie 2009-06-15
  • 打赏
  • 举报
回复
public class GetNumber {

public static char[] getChar(String s){
return s.toCharArray();
}

public static void main(String[] args){
String s = "12345678";
char[] c = getChar(s);
}

}
wangljgood 2009-06-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kaisa316 的回复:]
toCharArray()
[/Quote]
正解
如果输入的字符串有可能还含有其它的字符,那么还得加上判断
kaisa316 2009-06-15
  • 打赏
  • 举报
回复
toCharArray()

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧