字符串分隔

public class Test2 {

public static void main(String[] args){

String str1 ="the sky is blue";

String str2 ="  hello world!  ";

String str3 ="a good  example";

System.out.println(getBackStr(str1));

System.out.println(getBackStr(str2));

System.out.println(getBackStr(str3));

}

public static String getBackStr(String s){

String[] str = s.split(" ");

int length = str.length;

int cycCount = length /2+length %2;

for (int i =0; i < cycCount; i++) {

String temp = str[i];

str[i] = str[length-1-i];

str[length-1-i] = temp;

}

StringBuffer sb =new StringBuffer();

for(int i =0 ; i < str.length ; i++){

if(str[i].equals("")){

continue;

}

sb.append(str[i]).append(" ");

}

if(sb.length() !=0)

return sb.substring(0,sb.length()-1);

return "";

}

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 50道经典Java编程练习题,将数学思维运用到编程中来。抱歉哈找不到文章的原贴了,有冒犯的麻烦知会声哈~ 1.指数...
    OSET我要编程阅读 7,267评论 0 9
  • 包装类 包装类是把八大基本数据类型封装到一个类中,并提供属性和方法。 是为了更方便的操作基本数据类型,而不是取代基...
    什么时候养猫阅读 386评论 0 0
  • 从网上复制的,看别人的比较全面,自己搬过来,方便以后查找。原链接:https://www.cnblogs.com/...
    lxtyp阅读 1,424评论 0 9
  • 微分方程概要 一、一阶齐次线性微分方程 形如其通解为 二、一阶非齐次线性微分方程 形如其通解为 常数变易法推导过程...
    疯魔雪梨阅读 1,073评论 0 0
  • 海上明月铺光路,漫天星辰撒宝珠。 潮起潮落知不知,多少岁月随它去。
    简村小吹阅读 332评论 2 4

友情链接更多精彩内容