字符串的最常用操作

来源:互联网 发布:linux命令启动sh脚本 编辑:程序博客网 时间:2024/06/03 22:33
package lz.test.stringoperation;

public class StringOperation {
public static void main(String[] args) {
String testStr = "http://lz881228.blog.163.com";
//获取指定位置字符
System.out.println(testStr.charAt(4));
//获取字符子串.从第4位开始截取,到最后
System.out.println(testStr.substring(4));
//截取字符子串。从第2位开始到第5位之前的字符串
System.out.println(testStr.substring(2,5));
//替换字符。
System.out.println(testStr.replace("//", "\\"));
//得到字符串的哈希码
System.out.println(testStr.hashCode());
//获得字符所在的位置
System.out.println(testStr.indexOf("!"));
//如果不存在会返回-1
System.out.println(testStr.indexOf("$"));
//截取叹号后面的字符串
System.out.println(testStr.substring(testStr.indexOf("!")+1));
//字符串转化为字符数组
char[] strC = testStr.toCharArray();
for (char c : strC) {
System.out.print(c+"_");
}
//字符串转成数组
String str = "" ; 
 for(int i=0;i<4000;i++){
 str += "天童" + ",";
 strList.add(str);
 }
String [] strArray = str.split(",");

System.out.println();
//返回字符串中指定符号的下标值
testStr = "lz881228.doc";
System.out.println(testStr + ":" + testStr.lastIndexOf("."));
}
}

0 0
原创粉丝点击