1.用代码实现将一个字符串转成整型。例如 String a = "25869" 2.在一个字符串中寻找子字符串,如果该字符串存在这个子字符串,则输出这个子字符串的起始位置,否则输出-1以上两题均不可以使用库函数。
解决方案 »
- 往LsitView中增加HeaderView问题
- Android GSensor游戏的方向问题—高手进!
- 新手请教,在android想实现像卡拉ok一样的字幕显示效果,该如何实现?
- android 正则表达式 获取指定字符间的字符串
- 自定义VIEW,加载出错,求指点
- ImageButton中src和background区别
- Android 里面怎么读取短信设置的铃声 ? 求大神帮忙哇 ...
- Unable to execute dex: java.nio.BufferOverflowException.
- 【求助】Android 4.4.2 的WebView的问题
- 如何制作类似于excel的软件
- Android客户端莫名其秒阻塞
- AsyncTask用法求助
throws NumberFormatException
{
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/ if (s == null) {
throw new NumberFormatException("null");
} if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
} if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
} int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit; if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s); if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
} char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount); for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
while (++i <= max && source[i] != first);
} /* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j] ==
target[k]; j++, k++); if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}
C++;
CString str="123456";
int iLength=str.GetLength() ;
char *chr=new char[iLength];
chr=str.GetBuffer();
int RatValue=0;
int iTmp;
int i,j;
for (i=0;i<iLength;i++)
{
iTmp=1;//计算10的次方,因为不准用函数库
for (j=0;j<iLength-i-1;j++)
{
iTmp=iTmp*10;
}
RatValue=RatValue + ((chr[i]-48)* iTmp);
}
delete []chr;
return RatValue;
第二题也差不多,五分钟内也可以搞定.
第一题,如果JAVA的代码多于十行代码,说明,C还是挺优的.
C关键代码以上楼,也就是五句代码搞定啦.