黑马程序员技术交流社区
标题:
我按照毕老师做的myTrim()方法 将两端的空格给去掉,为什...
[打印本页]
作者:
马超
时间:
2012-5-24 15:44
标题:
我按照毕老师做的myTrim()方法 将两端的空格给去掉,为什...
package com.itcast.String;
我按照毕老师做的myTrim()方法 将两端的空格给去掉,为什么实现不了,老是出错啊?
public class StringDemo01
{
public static void sop(String str)
{
System.out.println(str);
}
public static void main(String[] args)
{
String s = " ab cd ";
sop(s);
s = myTrim(s);
sop(s);
}
public static String myTrim(String str)
{
int start = 0,end = str.length()-1;
while(start<=end && str.charAt(start)== " ") **** 错误提示消息: Incompatible operand types char and String
start++;
while(start<=end && str.charAt(end) == " ") **** 同上
end--;
//返回有效字符串:
return str.substring(start,end+1);
}
}
求助。。
作者:
马超
时间:
2012-5-24 16:30
戴敏智 发表于 2012-5-24 16:22
因为charAt方法是String类中获取字符的方法,str.charAt(start)== " "语句中将获取的字符与字符串比较,显 ...
老犯一些不应该犯的错误。。
作者:
3108007412
时间:
2012-8-19 10:06
好东西 顶起!!!
作者:
舒远
时间:
2012-9-6 20:03
public class StringDemo
{
public static void sop(Object o)
{
System.out.println(o);
}
public static void main(String[] args)
{
String s = " ab cd ";
sop(s);
s = myTrim(s);
sop(s);
sop(s.length());
}
public static String myTrim(String str)
{
return str.replaceAll("(^\\s*)|(\\s*$)","");//给用正则表达式实现一个。。
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2