O*z
2 楼
在自学java,用eclipse写了下面的简单的代码,要实现的功能是:如果一个字符串中的
前两个字符和最后两个字符相同,则去掉最前面两个字符,返回其余的字符。但是下面
的代码就是不work,我觉得逻辑上没问题啊,请帮忙看看,谢谢
public class without2
{
public static void main(String[] args)
{
String str1 = "HelloHe";
System.out.println(rmfirsttwo(str1)); //expected result: lloHe
}
public static String rmfirsttwo(String str)
{
int strLength = str.length();
if(strLength <2)
{
return str;
}
else if(strLength == 2)
{
return "";
}
else
{
String firstTwo = str.substring(0,2);
if(str.endsWith(firstTwo))
{
return str.substring(2);
}
}
}
}
前两个字符和最后两个字符相同,则去掉最前面两个字符,返回其余的字符。但是下面
的代码就是不work,我觉得逻辑上没问题啊,请帮忙看看,谢谢
public class without2
{
public static void main(String[] args)
{
String str1 = "HelloHe";
System.out.println(rmfirsttwo(str1)); //expected result: lloHe
}
public static String rmfirsttwo(String str)
{
int strLength = str.length();
if(strLength <2)
{
return str;
}
else if(strLength == 2)
{
return "";
}
else
{
String firstTwo = str.substring(0,2);
if(str.endsWith(firstTwo))
{
return str.substring(2);
}
}
}
}
相关阅读
JAVA 1.5 的web start console在哪藏着呢?java3d ModelClip question有关JBOSS-MQ问题求教 java.lang.outofMemory关于CMP问题如何改变JDK 的语言设置JDBC怎样连接FoxPro数据库?请高手指教:Tomcat JNDI problem中文XP突然装不上NetBeans怪事s*v != 0请教一个安全方面的MASTER题目Please helpwhere to execute java program?Anybody subscribe JavaOne Online member?eclipse question,please helpWhich Web service is better EJB or RPC怎么让客户端得知和得到数据库修改的数据?a garbage collection questionHow to handle invalid XML characterRe: Display, edit, and save Image on HTM