Java基础知识 14
String类
详细程序运行:E:\Java project\20200426-String-类-练习
字符串:是由多个字符组成的一串数据(字符序列)。
字符串可以看成是字符数组。
字符串的每个字符,从左往右是编有索引的,从0开始。
如果是String类,没有重写toString()方法,那就打印地址值。
如果不是地址值,那说明String类重写了toString()方法。
public class Mytest {
public static void main(String[] args) {
//字符串:是由多个字符组成的一串数据。(字符序列)
//字符串可以看成是字符数组
//字符串的每一个字符,从左往右是编有索引的,从0开始
//String类代表字符串:
//1.java程序中所有字符串字面值常量(如"abc")都作为此类的实例实现
//2.字符串是常量:它们的值在创建后不能更改
String s = new String();
//如果String类,没有重写toString()方法,那么就会打印地址值,如果不是地址值,那就说明String类重写了toString方法
//String类,其实重写了toString()方法,打印字符串的内容。
System.out.println(s.toString());
System.out.println(s);
System.out.println("abc");
System.out.println("");//空串
System.out.println("===================");
//public String (String Original)
//把字符串常量值转为字符串
String s1 = new String("123");
System.out.println(s1.toString());
System.out.println(s1);
}
}
String构造方法
详细程序运行:E:\Java project\20200426-String-类-练习
(1)public String(String original)----->把字符串常量值转为字符串
初始化一个新创建的String对象,使其表示一个与参数相同的字符序列,换句话说,新创建的字符串是该参数字符串的副本,由于String是不可变的,所以无需使用此类构造方法,除非需要Original的显式副本。
(2)public String(byte[] bytes)----->把字节数组转成字符串
通过使用平台的默认字符集解码指定的Byte数组,构造一个新的String,新的String的长度是子符集的函数,因此可能不等于byte数组的长度。
(3)public String(byte[] bytes,int offset,int length)—>(要解码为字符的byte,要解码的第一个byte的索引,要解码的byte数)
通过使用平台的默认子符集解码指定的byte子数组,构造一个新的String,新的String的长度是子符集的函数,因此可能不等于byte数组的长度。
public class Mytest {
public static void main(String[] args) {
System.out.println("");
String s = new String();
System.out.println(s.toString());
System.out.println(s);
String s1 = new String("666");
System.out.println(s1.toString());
System.out.println(s1);
System.out.println("------------------------");
//public String(byte[] bytes)
//把字节数组转成字符串
byte[] bytes={97,98,99,100,101,102,103,104};
String s2 = new String(bytes);
System.out.println(s2);
System.out.println("------------------------");
String s3 = new String(bytes, 0, 5);
System.out.println(s3.toString());
}
}
(4)public String(char[] value)
分配一个新的String,使其表示字符数组参数中当前包含的字符序列。
(5)public String(char[] value,int offset,int count)----->(作为字符源的数组,初始偏移量,长度)
分配一个新的String,它包含取自字符数组参数一个子数组的字符,offset参数是子数组第一个字符的索引,count参数指定子数组的长度。
public class Mytest2 {
public static void main(String[] args) {
//把字符数组转换成字符串
char[] chars={'我','是','中','国','人','我','爱','我','的','祖','国'};
//遍历
String s = new String();
for (int i = 0; i < chars.length; i++) {
s+=chars[i];
}
System.out.println(s);
System.out.println("------------------");
String s1 = new String(chars);
System.out.println(s1);
System.out.println("------------------");
String s2 = new String(chars, 2, 4);
System.out.println(s2);
}
}
String类代表字符串:
1.Java程序中所有字符串的字面值常量(如“abc”)都作为此类的实例实现。
2.字符串是常量:它们的值在创建后不能更改—>指的是值不能随便更改,但引用可以更改。
public class Mytest3 {
public static void main(String[] args) {
String s1 = new String("abcdefg");
//获取字符串的长度
int i = s1.length();
System.out.println(i);
System.out.println("--------------------");
// String 类代表字符串。
// 1.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
int length = "123456789".length();
int length1 = "01234567899876543210".length();
System.out.println(length);
System.out.println(length1);
System.out.println("--------------------");
//定义一个字符串
String s = "我是中国人,我爱我的祖国";
System.out.println(s.length());
System.out.println("--------------------");
String s2 = new String("我爱Java");
System.out.println(s2.length());
}
}
public class Mytest {
public static void main(String[] args) {
//字符串是常量,它们的值在创建之后不能再更改。
String s="hello";
s="我"+"爱"+"java";
System.out.println(s);
}
}
String类的判断功能
详细程序运行:E:\Java project\20200427-String-类-练习
(1)判断两个字符串,字面上的内容是否相同,区分大小写
boolean b=“abc”.equals(“ABC”)
(2)判断两个字符串,字面上的内容是否相同,不区分大小写
boolean b=“abc”.equalsIgnoreCase(“ABC”)
(3)判断一个字符串是不是空串
boolean b="".isEmpty()
(4)判断一个字符串是不是以它开头
boolean b=“中国”.startwith(“中”)
(5)判断一个字符串是不是以它结尾
boolean b=“张三”.endwith(“三”)
(6)判断一个字符串,是否包含了一个字串
boolean b=“陕西省西安市”.contains(“西安”)
public class Mytest2 {
public static void main(String[] args) {
//判断两个字符串,字面内容是否相同,区分大小写
boolean s1 = "abc".equals("ABC");
System.out.println(s1);
//判断两个字符串,字面上的内容是否相同,不区分大小写
boolean s2 = "abc".equalsIgnoreCase("ABC");
System.out.println(s2);
//判断一个字符串是不是空串
boolean s3 = "".isEmpty();
System.out.println(s3);
//判断一个字符是不是以它开头
boolean s4 = "我是中国人".startsWith("我是");
System.out.println(s4);
//判断一个字符是不是以它结尾
boolean s5 = "尼古拉斯赵四".endsWith("赵四");
System.out.println(s5);
//判断一个字符串是否包含一个字串
boolean s6 = "我是中国人".contains("中国");
System.out.println(s6);
}
}
String类的获取功能
详细程序运行:E:\Java project\20200427-String-类-练习
(1)根据索引获取单个字符 遍历
charAt(索引),根据索引,来获取字符串的某个字符
//遍历
char c = s.charAt(i);
System.out.println©;
public class Mytest {
public static void main(String[] args) {
//判断一段字符串中大写字母,小写字母,数字出现的次数。
String s="wheudIhnoi253afwhefiAJOHFEWAIUHN1684351350";
//遍历,其中判断大小写,数字
int daxie=0;
int xiaoxie=0;
int num=0;
for (int i = 0; i < s.length(); i++) {
if((int)s.charAt(i)>64&&(int)s.charAt(i)<91){
daxie++;
}else if ((int)s.charAt(i)>96&&(int)s.charAt(i)<123){
xiaoxie++;
}else if((int)s.charAt(i)>47&&(int)s.charAt(i)<58){
num++;
}
char c = s.charAt(i);//遍历
System.out.println(c);
}
System.out.println(daxie);
System.out.println(xiaoxie);
System.out.println(num);
}
}
(2)查找该字符串或字符第一次出现的索引
int index=s.indexOf(‘我’)
字符串的索引以第一个字符索引为准
indexOf 从左往右,从开头找
(3)从指定索引处往后查找
int index=s.indexOf(“bc”,2)
调用indexOf(),如果没有找到,返回-1,我们经常用-1做判断条件
(4)从后往前找,该字符或字符串第一次出现的索引
int index=s.lastIndexOf(“我”,10)
(5)截取字符串,从指定索引处到末尾返回
String s1=s.subString(5)
(6)如果截取一段,可以指定两个索引
String s2=s.subString(0,6)//含头不含尾
public class Mytest3 {
public static void main(String[] args) {
//charAt(索引),根据索引,获取字符串中的单个字符
String s1="我是中国人我爱我的祖国";
char a = s1.charAt(5);
System.out.println(a);
//查找该字符串或者字符第一次出现的索引
//字符串中的索引以第一个字符索引为准,indexOf从左往右,从开头找。
String s2="中华人民共和国地图";
int b = s2.indexOf("民");
System.out.println(b);
//从指定索引处开始往后查找
int c = s2.indexOf("共",2);
System.out.println(c);
//调用indexOf(),如果没有找到,返回-1,我们常用-1作为判断条件。
int d = s2.indexOf("人", 5);
System.out.println(d);
//从后往前找,该字符或者字符串第一次出现的索引
int e = s2.lastIndexOf("国", 9);
System.out.println(e);
//截取字符串,从指定索引处到末尾返回
String f = s2.substring(4);
System.out.println(f);
//如果截取一段字符串,可以指定两个索引
String g = s2.substring(2, 7);
System.out.println(g);//含头不含尾
//怎样判断一个字在一个字符串中出现了一次
String s3="像我这样优秀的人本该灿烂过一生。";
int i = s3.indexOf("人");
int j = s3.lastIndexOf("人");
if(i==j){
System.out.println("这个字符在字符串中出现了一次");
}else{
System.out.println("这个字符在字符串中出现了多次");
}
}
}
String类的转换功能
详细程序运行:E:\Java project\20200427-String-类-练习
(1)把一个字符串转换成字节数组
String str=“abc”;
byte[] bytes=str.getBytes();
String s=new String(bytes);----->把字节数组转换成字符串
System.out.println(s);
把一个字符串,转换成字符数组 toCharArray()
String s=“中华人民共和国”;
char[] chars=s.toCharArray();
把一个字符数组转换成字符串:
String s1=new String(chars);
System.out.println(s1);
public class Mytest {
public static void main(String[] args) {
String s="abcdefg";
//把一个字符串转换成字节数组
byte[] bytes = s.getBytes();
System.out.println(bytes.length);
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
System.out.println("---------------------");
//把字节数组又转换成字符串
String s1 = new String(bytes);
System.out.println(s1);
}
}
(2)valueof()方法,可以把很多类型转换成字符串
String a=String.valueof(num);
String b=String.valueof(‘a’);
String c=String.valueof(3.14);
String d=String.valueof(false);
(3)把一个字符数组转换成字符串
String s1=String.valueof(new char[]{‘a’,‘b’,‘c’,‘d’});
String s2=new String(new char[]{‘a’,‘b’,‘c’});
(4)转化成大写
String s=“abcd”.toUpperCase();
转换成小写
String s=“ABCD”.toLowerCase();
(5)拼接字符串
String s=“aa”+“bb”+“cc”+“dd”;
String s=“aa”.concat(“bb”);
链式编程
String s=“aa”.concat(“bb”).concat(“cc”).concat(“dd”);
public class Mytest2 {
public static void main(String[] args) {
//valueOf()方法,可以把很多类型转换成字符串
String s = String.valueOf(100);
String s2 = String.valueOf('a');
String s3 = String.valueOf(3.14);
String s4 = String.valueOf(true);
//把一个字符数组转换成字符串
String s5 = String.valueOf(new char[]{'a', 'b', 'c', 'd', 'e'});
String s6 = new String(new char[]{'a', 'b', 'c'});
//小写字母转换成大写字母
String s7 = "abcd".toUpperCase();
//大写字母转换成小写字母
String s8 = "ABCD".toLowerCase();
//拼接字符串
String s9="aa"+"bb"+"cc"+"dd";
String s10 = "aa".concat("bb");
String concat = "aa".concat("bb").concat("cc").concat("dd").concat("ee");
}
}
(6)一次替换一个字符
String s=str.replace(“我”,"");
一次替换一个字符串
String s=str.replace(“我爱”,"").replace(“你是”,“小明”);
(7)去除字符串两端的空格,.trim()方法
String username=" shan xi sheng ";
String trim=username.trim();
public class Mytest {
public static void main(String[] args) {
String username=" zhangsan ";
//去除字符串两端的空格,trim()方法
String trim = username.trim();
System.out.println(trim);
System.out.println("----------------");
String mingzi1=" shan xi sheng ";
String a = mingzi1.trim();//去除两端的空格
System.out.println(a);
//去除所有的空格
String b = mingzi1.replace(" ", "");
System.out.println(b);
System.out.println("---------------------");
String mingzi=" shan xi sheng ";
byte[] bytes = mingzi.getBytes();
//去除左端的空格,先遍历,然后再遇到非空字符的时候,直接停止。
for (int i = 0; i < bytes.length; i++) {
//char c = mingzi.charAt(i);
//System.out.println(bytes[i]);
String replace = mingzi.replace(" ", "");
if(bytes[i]!=32) {
String s = new String(bytes);//转成字符串
String sub = s.substring(i);
System.out.println(sub);
break;
}
}
System.out.println("----------------------------------");
String mingzi2=" xi an shi ";
byte[] bytes2 = mingzi2.getBytes();
//去除左端的空格,先遍历,然后再遇到非空字符的时候,采用截取后半段,最后输出。
for (int i = 0; i < bytes2.length; i++) {
//char c = mingzi.charAt(i);
//System.out.println(bytes[i]);
if(bytes2[i]==32){
String replace = mingzi2.replace(" ", "");
}else if(bytes2[i]!=32) {
String s = new String(bytes2);//转成字符串
String sub = s.substring(i);
System.out.println(sub);
break;
}
}
System.out.println("-----------------------------");
//去掉右边的空格,先反向遍历,遇到非空字符的时候,然后截取,最后输出。
String name=" xi an shi ";
byte[] bytes3 = name.getBytes();
for (int i = bytes3.length-1; i >= 0; i--) {
if(bytes3[i]!=32){
String s = new String(bytes3);//转换成字符串
String sub2 = s.substring(0,i+1);
System.out.println(sub2);
break;
}
}
System.out.println("----------------------");
**//去掉字符串中间的空格(**有问题,随后修改**)**
String xingming=" xi an shi ";
byte[] bytes1 = xingming.getBytes();
for (int i = 0; i < bytes1.length; i++) {
if (bytes1[i] != 32) {
if (bytes1[i] == 32) {
int num2;
//String replace = xingming.replace(" ", "");
String s = new String(bytes1);
String A = s.substring(0, i);
//System.out.println(A);
}
}
if (bytes1[i] != 32) {
if (bytes1[i] == 32) {
int num2;
//String replace = xingming.replace(" ", "");
String s = new String(bytes1);
String B = s.substring(i, i);
//System.out.println(B);
}
}
if (bytes1[i] != 32) {
if (bytes1[i] == 32) {
int num2;
//String replace = xingming.replace(" ", "");
String s = new String(bytes1);
String C = s.substring(i, xingming.length()-1);
//System.out.println(C);
}
}
System.out.println("A".concat("B").concat("C"));
}
}
}
(8)比较两个字符是否相同,返回的是boolean类型。
boolean b=“abc”.equals(“ccc”);
比较两个字符串是否相同,返回的是int类型,是按照ASCII码值来比较的
int i=“ABC”.compareTo(“aBC”);
int num=“abc”.compareToIgnoreCase(“ABC”);