字符串:由多个字符组成的字符序列 字符序列是有索引的从0开始数
1.String 类代表字符串。Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。
-
字符串是常量;它们的值在创建之后不能更改。
String
创建String类对象
构建了一个内容为空的字符串对象 “” 空串
String s = new String();String 类重写了toString()方法,打印字符串的内容 System.out.println(s.toString()); System.out.println(s.length());
1.String 类代表字符串。Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。
String s1 = “abc”.toString();
System.out.println(s1);
int length = “abc”.length(); //获取字符串长度的方法
System.out.println(length);
System.out.println("".length());//0
String的特点:
一旦被创建就不能改变
String的特点:字符串是常量
一旦被创建就不能改变 指的是字符串得值不能被改变,你改变只是这个指向
public String(byte[] bytes):把字节数组转成字符串
字节数组
byte[] bytes={97,98,99,100};
创建一个字符串的实例,传入了一个字节数组
String s = new String(bytes);
打印字符串的内容
System.out.println(s.toString());
把字节数组的一部分转成字符串 bytes 要转换的字节数组,2,从哪个字节开始 2 转几个字节
String s1 = new String(bytes, 2, 2);
把字符数组转成字符串
char[] chars={'a','b','c','d','爱'};
String s = new String(chars);
String s1 = new String(chars, 2, 3);
//遍历字符数组,拼接成字符串
使用字面量赋值得方式定义字符串得时候,会现在字符串池中查找, 该字符串是否存在,如果存在,就不在重复开辟空间,使用以存在字符得引用
String s2 ="hello";
String s3="hello";
String s4="abc";
System.out.println(s2 == s3); //true
System.out.println(s3==s4);
//System.out.println(s1==s2);
String s1 = new String("hello2");
System.out.println(s2==s1);//false
String s9 = s1.intern();
System.out.println(s9);//hello2
System.out.println(s1==s9);//false
System.out.println(s2==s9); //false
==和equals() 的区别 ?
String 这个类认为父类Object中equals()方法比较两个对象得地址值,意义不是很大,所以String类
也重写了equals()方法,去比较两个字符串字面上得内容是否相同
字符串中常用的方法
//与判断相关的一些方法
boolean b = "abc".equals("ABC"); //判断两个字符串的内容是否相同,区分大写
//不区分大小的比较
boolean b1 = "abc".equalsIgnoreCase("ABc");
System.out.println(b1);
//判断一个字符串以什么开头
boolean startsWith = "王思聪".startsWith("");
System.out.println(startsWith);
//判断一个方法是否某字符串结尾
boolean b2 = "王思聪".endsWith("");
System.out.println(b2);
//判断一个字符串是不是空串
boolean b3 = "".isEmpty();
boolean b4 = "".length() == 0;
String str="abc";
//判断一个字符串,是否包含另一个字符串
boolean b5 = "往后余生,洗衣是你,做饭是你,带娃也是你".contains("余生2");
System.out.println(b5);
与获取相关的一些方法
//获取字符串的长度
int length = "abc".length();
//根据索引获取字符串中的某个字符
//StringIndexOutOfBoundsException 字符串索引 越界异常
String s = "abcdefg";
char ch = s.charAt(s.length()-1);
System.out.println(ch);
//获取这个字符,第一次在字符串出现的索引 indexOf() 从前往后检索
int index = "abcdefgafasdfljiodfasdfasfd".indexOf('g');
System.out.println(index);
//返回 -1 代表没有找到
int index2 = "abcdefgafasdfljiodfasdfasfd".indexOf("def2");
System.out.println(index2);
//lastIndexOf() 从后往前检索,该字符第一次在字符串中出现的索引
int last = "asdfasdfasdfefadfeafassdf".lastIndexOf('a');
System.out.println(last);
//我怎么判断一个字符,在该字符串中只出现过一次
String str="asfarsdfridfaerfadfeadfeadfeasdfe";
if(str.indexOf('i')==str.lastIndexOf('i')){
System.out.println(true);
}
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
System.out.println(c);
} //遍历字符串
//与转换相关的方法
```java
//大小写字母的转换
/*
String s = "abc".toUpperCase(); //转大写
String s1 = "ABC".toLowerCase();//转小写
//把一个字符串转换成字节数组
byte[] bytes = "abc".getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
//把 字节数组准换成字符串
String string = new String(bytes);
System.out.println(string);
*/
String str = "你好"; //utf-8 gbk Unicode 标准
byte[] bytes = str.getBytes();
//从字节数字中取出你
String s = new String(bytes, 0, 6);
System.out.println(s);
//把一个字符串转换成一个 字节数组
//把一个字符串转成字符数组 toCharArray()
String str="你们气死我了";//===> char[]{'你','们'}
char[] chars = str.toCharArray();
String 类里面有一个静态方法,可以把很多种类型转换成字符串类型
String s1 = String.valueOf(100);
String s2 = String.valueOf(3.14);
//把一个字符数组转换成功字符串 构造方法
//把一个字符串转换成字符数组
char[] chars = {'你', '们'}; //String str="你们";
String s3 = new String(chars, 0, 1);
char[] chars1 = s3.toCharArray();
String s4 = String.valueOf(chars, 0, 2);
// + 可以串拼串
String s5 = "aaa" + "bbb" + "cccc" + "dddd";
//String bbb = "aaa".concat("bbb");
String concat = "aaa".concat("bbb").concat("cccc").concat("dddd");
System.out.println(concat);
与替换相关的方法 一次替换一个字符
String s = "金正恩和特朗普和奥巴马都是国家元首".replace('金', '*');
System.out.println(s);
String s1 = "金正恩和特朗普和奥巴马都是国家元首".replace("金正恩", "***").replace("奥巴马", "*").replace("特朗普", "*");
System.out.println(s1);
//去掉字串两端空格 trim()
String str = " zhangsan ";
String trim = str.trim();
System.out.println(trim);
比较两个字符串的内容是否相同
boolean b = "abc".equals("abc");
System.out.println(b);
//按照字典顺序比,比差值
//通过字典顺序比不出来,就用长度比
int num = "abc".compareTo("abcdef");
// public int compareToIgnoreCase (String str)跟上面一样 只是忽略大小写的比较