1.1 String类概述
概述
java.lang.String 类代表字符串。Java程序中所有的字符串文字(例如 “abc” )都可以被看作是实现此类的实
例。
类 String 中包括用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取子字符串以及创建具有翻
译为大写或小写的所有字符的字符串的副本。
特点
- 字符串不变:字符串的值在创建后不能被更改。
public class DemoString {
public static void main(String[] args) {
String first = "Hello World";
first+="!";
System.out.println(first); //Hello World!
}
}
// 内存中有"Hello World","Hello World!"两个对象,first从指向"Hello World",改变指向,指向了"Hello World!
- 因为String对象是不可变的,所以它们可以被共享。
public class DemoString {
public static void main(String[] args) {
//判断内存数据是否被共享
String S1="test";
String S2="test";
System.out.println(S1);
System.out.println(S2);
System.out.println(S1==S2); //true
//因为new了两个不共的内存地址,所以为false
String S3= new String("test");
String S4= new String("test");
System.out.println(S3);
System.out.println(S4);
System.out.println(S3==S4); //false
}
}
输出:
test
test
true
test
test
false
- “abc” 等效于 char[] data={ ‘a’ , ‘b’ , ‘c’ }
public class DemoString {
public static void main(String[] args) {
String str="abc";
char[] cr1={'a','b','c'};
String chcr1=new String(cr1);
System.out.println("str:"+str);
System.out.println("chcr1:"+chcr1);
}
}
// String底层是靠字符数组实现的。
输入:
str:abc
chcr1:abc
1.2 使用步骤
查看类
java.lang.String :此类不需要导入。
查看构造方法
public String() :初始化新创建的 String对象,以使其表示空字符序列。
public String(char[] value) :通过当前参数中的字符数组来构造新的String。
public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的
String。
构造举例,代码如下:
// 无参构造
String strdemo1=new String();
// 通过字符数组构造
char[] chars={'a','b','c'};
String strdemo2=new String(chars);
// 通过字节数组构造
byte[] bytes={97,98,99};
String strdemo3=new String(bytes);
System.out.println("strdemo1:"+strdemo1);
System.out.println("strdemo2:"+strdemo2);
System.out.println("strdemo3:"+strdemo3);
输出:
strdemo1:
strdemo2:abc
strdemo3:abc
1.3 常用方法
判断功能的方法
public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小
写。
方法演示,代码如下:
package com.learn.Demo1;
public class StringequalsDemo1 {
public static void main(String[] args) {
//创建字符串对象
String s1="hello";
String s2="hello";
String s3="HELLO";
// boolean equals(Object obj):比较字符串的内容是否相同
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~");
//boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s1.equalsIgnoreCase(s3));
}
}
输出:
true
false
~~~~~~~~~~~~~~~~~~~~~~~~~~
true
true
Object 是” 对象”的意思,也是一种引用类型。作为参数类型,表示任意对象都可以传递到方法中。
获取功能的方法
public int length () :返回此字符串的长度。
public String concat (String str) :将指定的字符串连接到该字符串的末尾。
public char charAt (int index) :返回指定索引处的 char值。
public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符
串结尾。
public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到
endIndex截取字符串。含beginIndex,不含endIndex。
方法演示,代码如下:
public class Stringalldemo {
public static void main(String[] args) {
//创建字符串对象
String demo = "hello world";
// int length():获取字符串的长度,其实也就是字符个数
System.out.println(demo.length());
System.out.println("~~~~~~~~~~~~~~~~~~~~");
// String concat (String str):将将指定的字符串连接到该字符串的末尾.
System.out.println(demo.concat("!"));
System.out.println("~~~~~~~~~~~~~~~~~~~~");
// char charAt(int index):获取指定索引处的字符
System.out.println(demo.charAt(2));
System.out.println(demo.charAt(6));
// int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1
System.out.println(demo.indexOf("w"));
System.out.println("~~~~~~~~~~~~~~~~~~~~");
// String substring(int start):从start开始截取字符串到字符串结尾
System.out.println(demo.substring(3));
// String substring(int start,int end):从start到end截取字符串。含start,不含end。
System.out.println(demo.substring(2,5));
}
}
输出:
11
~~~~~~~~~~~~~~~~~~~~
hello world!
~~~~~~~~~~~~~~~~~~~~
l
w
6
~~~~~~~~~~~~~~~~~~~~
lo world
llo
转换功能的方法
public char[] toCharArray () :将此字符串转换为新的字符数组。
public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使
用replacement字符串替换。
方法演示,代码如下:
package com.learn.Demo1;
public class String_demo1 {
public static void main(String[] args) {
//创建字符串对象
String s = "hello world";
// char[] toCharArray():把字符串转换为字符数组
char[] chs = s.toCharArray();
for(int x = 0; x < chs.length; x++) {
System.out.print(x+":");
System.out.print(chs[x]);
System.out.print(", ");
}
System.out.println();
// byte[] getBytes ():把字符串转换为字节数组
byte[] bytes = s.getBytes();
for(int x = 0; x < bytes.length; x++) {
System.out.print(x+":");
System.out.print(bytes[x]);
System.out.print(", ");
}
System.out.println();
// 替换字母it为大写IT
String str = "itcast itheima";
String replace = str.replace("it", "IT");
System.out.println(replace); // ITcast ITheima
}
}
//CharSequence 是一个接口,也是一种引用类型。作为参数类型,可以把String对象传递到方法中
分割功能的方法
public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
方法演示,代码如下:
package com.learn.Demo1;
public class String_demo2 {
public static void main(String[] args) {
// 创建字符串
String demo ="aa|bb|cc";
System.out.println(demo);
String[] lists=demo.split("\\|");
for (int i = 0;i<lists.length;i++){
System.out.println(lists[i]);
}
}
}
输出:
aa|bb|cc
aa
bb
cc
//注意分割时如果遇到系统不识别字符 ,需要使用转义符“\\”
String 练习
统计字符个数
键盘录入一个字符,统计字符串中大小写字母及数字字符个数
package com.learn.Demo1;
import java.util.Scanner;
public class String_demo3 {
// 键盘录入一个字符,统计字符串中大小写字母及数字字符个数
public static void main(String[] args) {
System.out.println("input");
Scanner scanner=new Scanner(System.in);
String list=scanner.next();
System.out.println(list);
System.out.println("Number"+list.length());
int strmaxnum=0;
int strminnum=0;
int strnum=0;
for (int i=0; i < list.length();i++){
char temp=list.charAt(i);
System.out.print(temp);
if (temp >='A'&& temp<='Z'){
strmaxnum+=1;
}else if(temp >='a'&& temp<='z'){
strminnum+=1;
}else if(temp >='0'&& temp<='9'){
strnum+=1;
}
}
System.out.println();
System.out.println("strmaxnum"+strmaxnum);
System.out.println("strminnum"+strminnum);
System.out.println("strnum"+strnum);
}
}
视频百度下载地址:
链接:https://pan.baidu.com/s/1xUBhkIvgnM1BPB1Sgu9-YA 提取码:zfd9
视频全套教程购买地址:http://suo.im/5zgMaW