每天学完之后给自己一个总结,理解,希望我的笔记能帮助到大家。
String类的概述
java.lang.String 类代表字符串。Java程序中所有的字符串文字(例如 "abc" )都可以被看作是实现此类的实例。
类 String 中包括用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取子字符串以及创建具有翻译为大写或小写的所有字符的字符串的副本。
特点
1. 字符串不变:字符串的值在创建后不能被更改
2. 因为String对象是不可变的,所以它们可以被共享。
3. "abc" 等效于 char[] data={ 'a' , 'b' , 'c' }
常用的构造方法
1.public String() :初始化新创建的 String对象,以使其表示空字符序列。
2.public String(char[] value) :通过当前参数中的字符数组来构造新的String。
3.public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。
常用方法
1.public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
2.public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写。
代码示例
public classString_Demo01{
public staticvoidmain(String[]args) {
//创建字符串对象
Strings1="hello";
Strings2="hello";
Strings3="HELLO";
// boolean equals(Object obj):比较字符串的内容是否相同
System.out.println(s1.equals(s2));// true
System.out.println(s1.equals(s3));// false
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
//boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2));// true
System.out.println(s1.equalsIgnoreCase(s3));// true
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
}
}
获取功能的方法
1.public int length () :返回此字符串的长度。
2.public String concat (String str) :将指定的字符串连接到该字符串的末尾。
3.public char charAt (int index) :返回指定索引处的 char值。
4.public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
5.public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
6.public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex
方法演示,代码如下:
public class String_Demo02 {
public static void main(String[] args) {
//创建字符串对象
String s = "helloworld";
// int length():获取字符串的长度,其实也就是字符个数
System.out.println(s.length());
System.out.println("‐‐‐‐‐‐‐‐");
// String concat (String str):将将指定的字符串连接到该字符串的末尾.
String s = "helloworld";
String s2 = s.concat("**hello ");
System.out.println(s2);// helloworld**hello
// char charAt(int index):获取指定索引处的字符
System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println("‐‐‐‐‐‐‐‐");
// int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1
System.out.println(s.indexOf("l"));
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("ak"));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start):从start开始截取字符串到字符串结尾
System.out.println(s.substring(0));
System.out.println(s.substring(5));
System.out.println("‐‐‐‐‐‐‐‐");
// String substring(int start,int end):从start到end截取字符串。含start,不含end。
System.out.println(s.substring(0, s.length()));
System.out.println(s.substring(3,8));
}
}
06
转换功能的方法
转换功能的方法
1.public char[] toCharArray () :将此字符串转换为新的字符数组。
2.public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
3.public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。
public class String_Demo03 {
public static void main(String[] args) {
//创建字符串对象
String s = "abcde";
// char[] toCharArray():把字符串转换为字符数组
char[] chs = s.toCharArray();
for(int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
// byte[] getBytes ():把字符串转换为字节数组
byte[] bytes = s.getBytes();
for(int x = 0; x < bytes.length; x++) {
System.out.println(bytes[x]);
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
// 替换字母it为大写IT
String str = "itcast itheima";
String replace = str.replace("it", "IT");
System.out.println(replace); // ITcast ITheima
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
}
}
分割功能的方法
public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组
public class String_Demo03 {
public static void main(String[] args) {
//创建字符串对象
String s = "aa|bb|cc";
String[] strArray = s.split("|"); // ["aa","bb","cc"]
for(int x = 0; x < strArray.length; x++) {
System.out.println(strArray[x]); // aa bb cc
}
}
}
String的练习
/**
* 定义一个方法,把数组{1,2,3}按照指定个格式拼接成一个字符串。格式参照如下:[word1#word2#word3]
*/
public class Demo01StringTest {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
String reslut = arrayToString(arr);
System.out.println(reslut);
}
public static String arrayToString(int[] arr) {
String s = new String("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
s = s.concat("word"+arr[i] + "]");
} else {
s = s.concat("word" + arr[i] + "#");
}
}
return s;
}
}
import java.util.Scanner;
/*
键盘录入一个字符,统计字符串中大小写字母及数字字符个数
*/
public class Demo02StringTest02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的字符串");
String input = sc.next();
int SmallCount = 0;
int BigCount = 0;
int Number = 0;
int OtherCount = 0;
char[] charArray = input.toCharArray();
for (int i = 0; i < charArray.length; i++) {
//ch= charArray[i];
if ('A' < charArray[i] && charArray[i] < 'Z') {
BigCount++;
} else if ('a' < charArray[i] && 'z' > charArray[i]) {
SmallCount++;
} else if ('0' < charArray[i] && charArray[i] < '9') {
Number++;
} else {
OtherCount++;
}
}
System.out.println("大写字母有:" + BigCount);
System.out.println("小写字母有:" + SmallCount);
System.out.println("数字有:" + Number);
System.out.println("其他字符有:" + OtherCount);
}
}