java学习之字符串
时间:2021-04-21 12:02:46
收藏:0
阅读:0
Java字符串
-
三个字符串类
- String (不可变)
- StringBuilder (可变)
- StringBuffer(可变)
-
空字符串不是null,空字符串分配了空间,而null没有分配空间
-
不可变字符串的创建
package chapter8;
public class chapt01 {
public static void main(String[] args) {
// 创建字符串对象
String s1 = "";
String s2 = new String("hello world");
char[] chars = {‘a‘,‘b‘,‘c‘,‘d‘};
String s3 = new String(chars);
byte[] bytes = {97,98,99};
String s4 = new String(bytes);
String s5 = new String(chars,1,2);
}
}
- 在使用java.lang包时不需要导入该包,因为它是解释器自动引入的
- 字符串池:java中的String常量采用字符串池技术,字符串池是一种字符串驻留技术,例如我要查找“Hello”字符串常量,首先会在字符串池中寻找该对象,如果存在,直接将“Hello”赋值给字符常量,如果没有,就会创建“Hello”字符串对象,然后放到字符串池当中去,这种原理并不适用于用 new 关键字创建的字符串对象,用 new 关键字创建的每一个对象都会指向不同的地址空间。
- 字符串拼接
- +运算符 : 可以连接任何类型数据拼接成为字符串
- concat方法: 只能拼接String类型字符串
package chapter8;
import java.util.Date;
public class chapt02 {
public static void main(String[] args) {
String s1 = "hello ";
s1 += "world";
String s2 = "Hello";
s2 = s2.concat(" ").concat("World");
Date time = new Date();
System.out.println(s1);
System.out.println(s2);
// 对象拼接自动采用toString()方法
System.out.println("Today is " + time);
}
}
- 字符串本质上是字符数组,因此它也有所引,索引从零开始,String的charAt(int index)方法可以返回索引index的位置的字符
- 字符串查找
package chapter8;
public class chapt03 {
public static void main(String[] args) {
String sourceStr = "this is a string accessing example";
int firstChar = sourceStr.indexOf(‘r‘);
int secondChar = sourceStr.indexOf(‘r‘,20);
// 表达式的值从索引值为20开始往后面搜索
int firstString = sourceStr.indexOf("is");
int secondString = sourceStr.indexOf("is",5);
int lastChar1 = sourceStr.lastIndexOf(‘r‘);
int lastChar2 = sourceStr.lastIndexOf(‘r‘,5);
int lastString1 = sourceStr.lastIndexOf("is");
int lastString2 = sourceStr.lastIndexOf("is",2);
int length = sourceStr.length();
}
}
- 字符串比较
- 比较相等
- boolean equals(Object anObject) 比较两个字符串的内容是否相等
- boolean equalsIgnoreCase(String anotherString) 忽略大小写
- 比较大小
- int compareTo(String str) 按照字典序比较两个字符串,如果大于参数字符串就返回大于0,小于参数字符串就返回大于0的值,相等则返回0
- int compareToIgnore(String str) 忽略大小写的情况
- 比较前缀和后缀
- boolean endsWith(String suffix) 后缀比较
- boolean startWith(String prefix) 前缀比较
- 比较相等
package chapter8;
public class chapt04 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("HELLO");
// 比较两个字符串的内容是否相同 true
System.out.println(s1.equals(s2));
// 比较两个字符串是否为相同的引用 false
System.out.println(s1 == s2);
// 忽略大小写的情况 false true
System.out.println(s1.equals(s3));
System.out.println(s1.equalsIgnoreCase(s3));
String s4 = new String("python");
String s5 = new String("java");
String s6 = new String("go");
// 比较两个字符串的大小
// s5 < s4 返回小于0的数字 , s5 > s6 返回大于零的数字
System.out.println(s5.compareTo(s4));
System.out.println(s5.compareTo(s6));
// 忽略大小写的字符串比较 返回值为0
System.out.println(s4.compareToIgnoreCase("PYTHON"));
// 比较前缀和后缀
String[] fileFolder = {"java.doc","python.xls","hello.xls","go.doc","html.doc"};
int wordCounter = 0;
for(String file : fileFolder){
file = file.trim();
if(file.endsWith(".doc")) {
wordCounter++;
}
}
int xlsCounter = 0;
for (String file : fileFolder){
file = file.trim();
if (file.endsWith(".xls")){
xlsCounter++;
}
}
System.out.println("文档数量是:" + wordCounter);
System.out.println("表格数量是:" + xlsCounter);
}
}
- 字符串截取
- String substring (int beginIndex)
- String substring(int beginIndex , int endIndex)
package chapter8;
public class chapt05 {
public static void main(String[] args) {
String sourceStr = new String("There is a string accessing example");
String subStr1 = sourceStr.substring(28);
String substr2 = sourceStr.substring(11,17);
// %n是换行的格式字符串,只能用于print输出语句当中
// \n也是换行的转义字符,可用于任何字符串
System.out.printf("subStr1 == %s%n",subStr1);
System.out.printf("subStr2 == %s%n",substr2);
// 分割字符串
String[] array = sourceStr.split(" ");
System.out.println("调用Split()方法....");
for (String item : array){
System.out.printf("%s%n",item);
}
}
}
- 可变字符串
- 可变字符串在追加,删除,修改,插入,拼接的过程中不会产生新的对象
- StringBuffer是线程安全的,它的方法支持线程同步,线程同步会操作串行顺序执行,在单线程的环境下影响效率
- StringBuilder是StringBuffer的单线程版,它不是线程安全的,但是执行效率很高
- 字符串长度和字符串缓冲区容量的区别:
- 字符串长度所指的是字符串本身在字符串缓冲区所包含的字符串长度,通过length()获得
- 字符串缓冲区容量指的是缓冲区所能容纳的最大字符,通过capacity()获得
- 当容纳的字符超过缓冲区的容量时,缓冲区自动扩充容量,但这是以牺牲性能为代价的
package chapter8;
public class chapt06 {
public static void main(String[] args) {
StringBuilder builder1 = new StringBuilder();
System.out.println("length == " + builder1.length());
System.out.println("capacity == " + builder1.capacity());
StringBuilder builder2 = new StringBuilder("hello");
System.out.println("length == " + builder2.length());
System.out.println("capacity == " + builder2.capacity());
}
// 缓冲区的默认长度为16,超过容量会自动扩容
StringBuilder builder3 = new StringBuilder();
}
- 字符串追加
- append()
package chapter8;
public class chapt07 {
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder("Hello");
s1.append(" ").append("world");
s1.append(".");
System.out.println(s1);
// 添加布尔值 空对象 转义符
StringBuilder s2 = new StringBuilder();
Object object = null;
// 布尔值false转换为"false" , null转换为"null"
s2.append(false).append(‘\t‘).append(object);
System.out.println(s2);
// 添加数值
StringBuilder s3 = new StringBuilder();
for (int i = 0;i < 10; i++){
s3.append(i);
}
System.out.println(s3);
}
}
- 字符串插入,删除,替换
- StringBuilder insert(int offset , String str): 在字符串缓冲区搜索索引为offset的字符位置之前插入str,insert有很多重载方法,可以插入任何类型的数据
- StringBuilder delete(int start , int end): 在字符串缓冲区里面删除从start到end-1的字符·
- StringBuilder replace(int start, int end , String str) : 在字符串缓冲区用 str 替换从 位置start 到 end-1的子字符串
- StringBuilder方法在StringBuffer中也同样适用
package chapter8;
public class chapt08 {
public static void main(String[] args) {
String str1 = "Java C";
StringBuilder mstr = new StringBuilder(str1);
// 插入字符串
mstr.insert(4," C++");
System.out.println(mstr);
// 追加字符串
mstr.insert(mstr.length()," Python");
mstr.append(" PHP");
System.out.println(mstr);
// 删除字符串
mstr.delete(11,23);
System.out.println(mstr);
}
}
评论(0)