java-string
1、String
1.1 代码
package com.nash.java.pop.ch05_string;
/**
* 字符串 String
* 1、值不可变、引用可变
*/
public class StringApp {
public static void main(String[] args){
// 1) 字符串定义
String str = "hello world hello new world ";
String str2 = "hello java ";
String str3 = "hello world ";
// 2) 字符串操作
// 返回指定位置的字符
System.out.println("charAt():\t " + str.charAt(0));
// 返回指定位置的字符的Unicode编码
System.out.println("codePointAt():\t " + str.codePointAt(0));
// 拼接字符串
System.out.println("concat():\t " + str.concat(str2));
System.out.println("+:\t " + str+str2);
// 是否包含某子串
System.out.println("contains():\t " + str.contains("hello"));
// 比较字符串对象(内存地址)
System.out.println("equals():\t " + str.equals(str2));
// 比较字符串内容
System.out.println("equalsIgnoreCase():\t " + str.equalsIgnoreCase(str3));
// 获取hashCode
System.out.println("hashCode():\t " + str.hashCode());
// 返回某子串首次出现的位置
System.out.println("indexOf():\t " + str.indexOf("world"));
// 返回字符串长度
System.out.println("length():\t " + str.length());
// 正则匹配
System.out.println("matches():\t " + str.matches("^hello.*"));
// 替换
System.out.println("replace():\t " + str.replace("hello","replaced"));
System.out.println("replaceAll():\t " + str.replaceAll("hello","replaced"));
// 切割(指定分隔符)
String[] splits = str.split(" ");
for (String split:splits){
System.out.println("split():\t " + split);
}
// 拼接(指定分隔符)
System.out.println("join():\t " + String.join("--",str,str2));
// 子串
System.out.println("substring():\t " + str.substring(6));
// 大写
System.out.println("toUpperCase():\t " + str.toUpperCase());
// 去除头尾空格
System.out.println("trim():\t " + str.trim());
}
}
1.2 结果
charAt(): h
codePointAt(): 104
concat(): hello world hello new world hello java
+: hello world hello new world hello java
contains(): true
equals(): false
equalsIgnoreCase(): false
hashCode(): 1881745208
indexOf(): 6
length(): 28
matches(): true
replace(): replaced world replaced new world
replaceAll(): replaced world replaced new world
split(): hello
split(): world
split(): hello
split(): new
split(): world
join(): hello world hello new world --hello java
substring(): world hello new world
toUpperCase(): HELLO WORLD HELLO NEW WORLD
trim(): hello world hello new world
2、StringBuffer
2.1 代码
package com.nash.java.pop.ch05_string;
/**
* 字符串缓冲 StringBuffer
* 1、值可变、引用可变
*/
public class StringBufferApp {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());
StringBuffer sb2=new StringBuffer("abc");
System.out.println(sb2.capacity());
StringBuffer sb3=new StringBuffer(100);
System.out.println(sb3.capacity());
StringBuffer sb4=new StringBuffer("Hello");
System.out.println(sb4.hashCode());
sb4.append(",");
System.out.println(sb4.hashCode());
sb4.append("world!\n");
System.out.println(sb4.hashCode());
sb4.append("welcome to ");
System.out.println(sb4.hashCode());
sb4.append("www.java1995.com");
System.out.println(sb4.hashCode());
System.out.println(sb4.toString());
}
}
2.2 结果
16
19
100
356573597
356573597
356573597
356573597
356573597
Hello,world!
网友评论