【IT笔试面试题整理】字符串转数组+数组转字符串

【试题描述】定义一个函数,字符串转数组数组转字符串

【参考代码】

 1     public static int strToInt(String str)
2 {
3 int i = 0, num = 0;
4 char[] strTemp = str.toCharArray();
5 boolean isNeg = false;
6 int len = str.length();
7
8 if (strTemp[0] == '-')
9 {
10 isNeg = true;
11 i = 1;
12 }
13 while (i < len)
14 {
15 num *= 10;
16 num += (strTemp[i++] - '0');
17 }
18 if (isNeg)
19 num *= -1;
20 return num;
21 }
 1     public static String intTOStr(int num)
2 {
3 char[] tempChar = new char[11];
4 int i = 0;
5 boolean isNeg = false;
6 if (num < 0)
7 {
8 num *= -1;
9 isNeg = true;
10 }
11 do{
12 tempChar[i++] = (char) (num % 10 + '0');
13 num /=10;
14 }while (num != 0);
15
16 if(isNeg)
17 tempChar[i] = '-';
18 System.out.println(tempChar);
19 StringBuilder b = new StringBuilder();
20 while(i>=0)
21 {
22 b.append(tempChar[i--]);
23 }
24
25 return b.toString();
26 }
上一篇:xml基础学习笔记01


下一篇:Python Anaconda使用