第七周技术博客
2016-04-07 15:07
316 查看
/** this is a class to deal with String, its provided with Trim, reverse etc @author Lu Shan @version v1.8 */ public class StrTool { /** trim: to remove the spaces on the two sides of the String @param temp to get a String @return return the new String */ public static String myTrim(String temp) { int i=0; while(temp.charAt(i)==' ') { i++; } int start=i; i=temp.length()-1; while(temp.charAt(i)==' ') { i--; } int end=i; String s = temp.substring(start,end+1); return s; } /** reverse temp from index i to index j @param temp to get a String @param i to get the start index @param j to get the end index @return to return the reversed String */ public static String reverse(String temp,int i,int j) { char[] arr= temp.toCharArray(); char mid; int m= j-1; for(int n=i;n<j;n++) { mid = arr[n-1]; arr[n-1]=arr[m]; arr[m--]=mid; } String s = String.valueOf(arr); return s; } /** return the times a str showed in another str @param t1 to get the longer String @param t2 to get the shorter String @return to return the times t2 shown in t1 */ public static int timesShow(String t1,String t2) { int num=-1; int index=0; while (index!=-1) { num++; if (num>=1) { index=t1.indexOf(t2,t2.length()+index); } else { index=t1.indexOf(t2,index); } } return num; } /** find the greatest substring of str1 in str2 ever existed @param str1 to get the longer String @param str2 to get the shorter String @return to return the largest substring of str2 in str1 */ public static String minorStr(String str1,String str2) { String temp; if(str1.contains(str2)) return str2; int start=0; int end=str2.length()-1; int sublength=str2.length()-1; while (sublength!=0) { for(;end<=str2.length();start++,end++) { char[] arr = str2.toCharArray(); if(str1.contains(str2.substring(start,end))) return str2.substring(start,end); } sublength--; start=0; end=sublength; } return ""; } /** System.out.println(); @param temp to get the thing to be printed */ public static void sop(Object temp) { System.out.println(temp); } } /* class StrToolDemo { public static void main(String[] args) { String str1 = "lushan in the house do you know that"; // String str2 = StrTool.myTrim(str1); // StrTool.sop(str2); // String str2 = StrTool.reverse(str1,2,4); //int num1 = StrTool.timesShow(str1,"lu"); String str2 = "in the house"; String str3 = StrTool.minorStr(str1,str2); StrTool.sop(str3); } } */
主要是自定义的字符串处理的class 自定义了本来就存在的trim函数 还有一些自定义的可以自己看说明书啊哈哈 不一一介绍了,最后main里面测试了一下,其实实现翻转的时候或许可以再封装一个函数
相关文章推荐
- PHP 二维array转换json(json数组)
- 安卓弹出窗口popupwindow
- Linux
- mysql 安装
- 6.4 bounce模块
- 内部类和匿名内部类
- Gson使用总结
- 苹果的CoreFoundation部分源代码
- rsyslog+analyzer+mysql实现日志收集展示
- 棋盘型动态规划 方格取数
- Mybatis——Mapper动态代理
- jsp页面分享到QQ空间、微信、微博、人人网等
- zigbee学习之路(十二):zigbee协议原理介绍
- hibernate的各种保存方式的区别 (save,persist,update,saveOrUpdte,merge,flush,lock)等
- 【dotnet跨平台】dotnet-cli典型场景
- 【读书笔记】深入理解计算机系统(第七章)
- STL学习笔记— —容器deque
- zz:Android 测试工具Monkey & Monkeyrunner 使用方法
- java 回调函数的入门Demo
- 第十三章 字符串