Java基础:String类
相关阅读
Java基础:String类Java字符串格式化Java基础:正则表达式
1. 概述
字符串是由多个字符组成的一串数据(字符序列),字符串可以看成是字符数组。
在实际开发中,字符串的操作是最常见的操作,没有之一。而Java没有内置的字符串类型,所以,就在Java类库中提供了一个类String 供我们来使用。String 类代表字符串。
2. String类的特点
字符串是常量,它的值在创建之后不能更改Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。字符串如果是变量相加,先开空间,在拼接。字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
package cn.itcast_02;
public class StringDemo {
public static void main(String[] args) {
String s =
"hello";
s +=
"world";
System.out.println(
"s:" + s);
}
}
字符串在内存中1
字符串在内存中
字符串在内存中2
字符串在内存中
3. 常见操作方法
3.1 构造方法
方法说明
String()创建一个内容为空的字符串String(byte[])根据指定的字节数组创建对象String(byte[],int,int)根据字节数组的一部分创建对象String(char[])根据指定的字符数组创建对象String(char[],int,int)根据字符数组的一部分创建对象String(String)根据指定的字符串内容创建对象
package cn.itcast_01;
public class StringDemo {
public static void main(String[] args) {
String s1 =
new String();
System.out.println(
"s1:" + s1);
System.out.println(
"s1.length():" + s1.length());
System.out.println(
"--------------------------");
byte[] bys = {
97,
98,
99,
100,
101 };
String s2 =
new String(bys);
System.out.println(
"s2:" + s2);
System.out.println(
"s2.length():" + s2.length());
System.out.println(
"--------------------------");
String s3 =
new String(bys,
1,
3);
System.out.println(
"s3:" + s3);
System.out.println(
"s3.length():" + s3.length());
System.out.println(
"--------------------------");
char[] chs = {
'a',
'b',
'c',
'd',
'e',
'爱',
'林',
'亲' };
String s4 =
new String(chs);
System.out.println(
"s4:" + s4);
System.out.println(
"s4.length():" + s4.length());
System.out.println(
"--------------------------");
String s5 =
new String(chs,
2,
4);
System.out.println(
"s5:" + s5);
System.out.println(
"s5.length():" + s5.length());
System.out.println(
"--------------------------");
String s6 =
new String(
"abcde");
System.out.println(
"s6:" + s6);
System.out.println(
"s6.length():" + s6.length());
System.out.println(
"--------------------------");
String s7 =
"abcde";
System.out.println(
"s7:"+s7);
System.out.println(
"s7.length():"+s7.length());
}
}
运行结果:
String字符串
3.2 判断功能
方法说明
equals()比较字符串的内容是否相等,区分大小写equalsIgnoreCase()比较字符串的内容是否相等,忽略大小写contains(String str)判断大字符串中是否包含小字符串startsWith()判断字符串是否以某个字符串开头endsWith()判断字符串是否以某个字符串结尾isEmpty()判断字符串是否为空
package cn.itcast_03;
public class StringDemo {
public static void main(String[] args) {
String s1 =
"helloworld";
String s2 =
"helloworld";
String s3 =
"HelloWorld";
System.out.println(
"equals:" + s1.equals(s2));
System.out.println(
"equals:" + s1.equals(s3));
System.out.println(
"-----------------------");
System.out.println(
"equals:" + s1.equalsIgnoreCase(s2));
System.out.println(
"equals:" + s1.equalsIgnoreCase(s3));
System.out.println(
"-----------------------");
System.out.println(
"contains:" + s1.contains(
"hello"));
System.out.println(
"contains:" + s1.contains(
"hw"));
System.out.println(
"-----------------------");
System.out.println(
"startsWith:" + s1.startsWith(
"h"));
System.out.println(
"startsWith:" + s1.startsWith(
"hello"));
System.out.println(
"startsWith:" + s1.startsWith(
"world"));
System.out.println(
"-----------------------");
System.out.println(
"isEmpty:" + s1.isEmpty());
String s4 =
"";
String s5 =
null;
System.out.println(
"isEmpty:" + s4.isEmpty());
System.out.println(
"isEmpty:" + s5.isEmpty());
}
}
运行结果:
String字符串
3.3 获取功能
方法说明
length()获取字符串长度charAt(int index)获取指定位置的字符indexOf(int ch)字符第一次出现的索引indexOf(String str)字符串第一次出现的索引indexOf(int ch,int fromIndex)字符从指定位置后第一次出现的索引indexOf(String str,int from)字符串从指定位置后第一次出现的索引lastIndexOf()字符串最后一次出现的索引subString(int start)从指定位置开始截取字符串subString(int start,int end)截取字符串,包左不包右
package cn.itcast_04;
public class StringDemo {
public static void main(String[] args) {
String s =
"helloworld";
System.out.println(
"s.length:" + s.length());
System.out.println(
"----------------------");
System.out.println(
"charAt:" + s.charAt(
7));
System.out.println(
"----------------------");
System.out.println(
"indexOf:" + s.indexOf(
'l'));
System.out.println(
"----------------------");
System.out.println(
"indexOf:" + s.indexOf(
"owo"));
System.out.println(
"----------------------");
System.out.println(
"indexOf:" + s.indexOf(
'l',
4));
System.out.println(
"indexOf:" + s.indexOf(
'k',
4));
System.out.println(
"indexOf:" + s.indexOf(
'l',
40));
System.out.println(
"----------------------");
System.out.println(
"substring:" + s.substring(
5));
System.out.println(
"substring:" + s.substring(
0));
System.out.println(
"----------------------");
System.out.println(
"substring:" + s.substring(
3,
8));
System.out.println(
"substring:" + s.substring(
0, s.length()));
}
}
运行结果:
String字符串
3.4 转换功能
方法说明
getBytes()把字符串转成字节数组getCharArray()把字符串转成字符数组valueOf(char[] chs)把字符数组转成字符串valueOf(int i)把int类型的数据转成字符串toLowerCase()把字符串转成小写toUpperCase()把字符串转成大写concat(String str)字符串拼接
package cn.itcast_05;
public class StringDemo {
public static void main(String[] args) {
String s =
"JavaSE";
byte[] bys = s.getBytes();
for (
int x =
0; x < bys.length; x++) {
System.out.println(bys[x]);
}
System.out.println(
"----------------");
char[] chs = s.toCharArray();
for (
int x =
0; x < chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println(
"----------------");
String ss = String.valueOf(chs);
System.out.println(ss);
System.out.println(
"----------------");
int i =
100;
String sss = String.valueOf(i);
System.out.println(sss);
System.out.println(
"----------------");
System.out.println(
"toLowerCase:" + s.toLowerCase());
System.out.println(
"s:" + s);
System.out.println(
"toUpperCase:" + s.toUpperCase());
System.out.println(
"----------------");
String s1 =
"hello";
String s2 =
"world";
String s3 = s1 + s2;
String s4 = s1.concat(s2);
System.out.println(
"s3:"+s3);
System.out.println(
"s4:"+s4);
}
}
运行结果:
String字符串
3.5 其他功能
方法说明
replace(char old,char new)替换字符replace(String old,String new)替换字符串trim()去掉字符串两端空格compareTo()按字典顺序比较字符串compareToIngnoreCase()按字典顺序比较字符串,忽略大小写format()格式化字符串
String.format(Locale.CHINA,
"第d条数据%s",
10,
"str")
更多关于字符串格式化的内容,请看 Java字符串格式化String.format()的使用
package cn.itcast_06;
public class StringDemo {
public static void main(String[] args) {
String s1 =
"helloworld";
String s2 = s1.replace(
'l',
'k');
String s3 = s1.replace(
"owo",
"ak47");
System.out.println(
"s1:" + s1);
System.out.println(
"s2:" + s2);
System.out.println(
"s3:" + s3);
System.out.println(
"---------------");
String s4 =
" hello world ";
String s5 = s4.trim();
System.out.println(
"s4:" + s4 +
"---");
System.out.println(
"s5:" + s5 +
"---");
String s6 =
"hello";
String s7 =
"hello";
String s8 =
"abc";
String s9 =
"xyz";
System.out.println(s6.compareTo(s7));
System.out.println(s6.compareTo(s8));
System.out.println(s6.compareTo(s9));
}
}
运行结果:
String字符串
4. String类练习
4.1 把数组中的数据按照指定个格式拼接成一个字符串
package cn.itcast_07;
public class StringTest2 {
public static void main(String[] args) {
int[] arr = {
1,
2,
3 };
String result = arrayToString(arr);
System.out.println(
"最终结果是:" + result);
}
public static String arrayToString(int[] arr) {
String s =
"";
s +=
"[";
for (
int x =
0; x < arr.length; x++) {
if (x == arr.length -
1) {
s += arr[x];
s +=
"]";
}
else {
s += arr[x];
s +=
", ";
}
}
return s;
}
}
4.2 字符串反转
package cn.itcast_07;
import java.util.Scanner;
public class StringTest3 {
public static void main(String[] args) {
Scanner sc =
new Scanner(System.in);
System.out.println(
"请输入一个字符串:");
String line = sc.nextLine();
String s = myReverse(line);
System.out.println(
"实现功能后的结果是:" + s);
}
public static String myReverse(String s) {
String result =
"";
char[] chs = s.toCharArray();
for (
int x = chs.length -
1; x >=
0; x--) {
result += chs[x];
}
return result;
}
}
4.3 统计大串中小串出现的次数
package cn.itcast_07;
public class StringTest5 {
public static void main(String[] args) {
String maxString =
"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
String minString =
"java";
int count = getCount(maxString, minString);
System.out.println(
"Java在大串中出现了:" + count +
"次");
}
public static int getCount(String maxString, String minString) {
int count =
0;
int index;
while((index=maxString.indexOf(minString))!=-
1){
count++;
maxString = maxString.substring(index + minString.length());
}
return count;
}
}
5. StringBuffer
我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题
StringBuffer是线程安全的可变字符序列。StringBuffer和String的区别?
前者长度和内容可变,后者不可变。如果使用前者做字符串的拼接,不会浪费太多的资源。
6. 常见操作方法
String字符串
6.1 构造方法和获取方法
package cn.itcast_01;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer sb =
new StringBuffer();
System.out.println(
"sb:" + sb);
System.out.println(
"sb.capacity():" + sb.capacity());
System.out.println(
"sb.length():" + sb.length());
System.out.println(
"--------------------------");
StringBuffer sb2 =
new StringBuffer(
50);
System.out.println(
"sb2:" + sb2);
System.out.println(
"sb2.capacity():" + sb2.capacity());
System.out.println(
"sb2.length():" + sb2.length());
System.out.println(
"--------------------------");
StringBuffer sb3 =
new StringBuffer(
"hello");
System.out.println(
"sb3:" + sb3);
System.out.println(
"sb3.capacity():" + sb3.capacity());
System.out.println(
"sb3.length():" + sb3.length());
}
}
6.2 添加功能
package cn.itcast_02;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer sb =
new StringBuffer();
sb.append(
"hello").append(
true).append(
12).append(
34.56);
System.out.println(
"sb:" + sb);
sb.insert(
5,
"world");
System.out.println(
"sb:" + sb);
}
}
运行结果:
String字符串
6.3 删除功能
package cn.itcast_03;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer sb =
new StringBuffer();
sb.append(
"hello").append(
"world").append(
"java");
System.out.println(
"sb:" + sb);
sb.delete(
0, sb.length());
System.out.println(
"sb:" + sb);
}
}
运行结果:
String字符串
6.4 替换功能
package cn.itcast_04;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer sb =
new StringBuffer();
sb.append(
"hello");
sb.append(
"world");
sb.append(
"java");
System.out.println(
"sb:" + sb);
sb.replace(
5,
10,
"节日快乐");
System.out.println(
"sb:" + sb);
}
}
运行结果:
String字符串
6.5 反转功能
package cn.itcast_05;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer sb =
new StringBuffer();
sb.append(
"霞青林爱我");
System.out.println(
"sb:" + sb);
sb.reverse();
System.out.println(
"sb:" + sb);
}
}
运行结果:
String字符串
6.6 截取功能
package cn.itcast_06;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer sb =
new StringBuffer();
sb.append(
"hello").append(
"world").append(
"java");
System.out.println(
"sb:" + sb);
String s = sb.substring(
5);
System.out.println(
"s:" + s);
System.out.println(
"sb:" + sb);
String ss = sb.substring(
5,
10);
System.out.println(
"ss:" + ss);
System.out.println(
"sb:" + sb);
}
}
运行结果:
String字符串
7. StringBuffer类练习
7.1 String和StringBuffer的相互转换
package cn.itcast_07;
public class StringBufferTest {
public static void main(String[] args) {
String s =
"hello";
StringBuffer sb =
new StringBuffer(s);
StringBuffer sb2 =
new StringBuffer();
sb2.append(s);
System.out.println(
"sb:" + sb);
System.out.println(
"sb2:" + sb2);
System.out.println(
"---------------");
StringBuffer buffer =
new StringBuffer(
"java");
String str =
new String(buffer);
String str2 = buffer.toString();
System.out.println(
"str:" + str);
System.out.println(
"str2:" + str2);
}
}
运行结果:
String字符串
7.2 把数组拼接成一个字符串
package cn.itcast_07;
public class StringBufferTest2 {
public static void main(String[] args) {
int[] arr = {
44,
33,
55,
11,
22 };
String s1 = arrayToString(arr);
System.out.println(
"s1:" + s1);
String s2 = arrayToString2(arr);
System.out.println(
"s2:" + s2);
}
public static String arrayToString2(int[] arr) {
StringBuffer sb =
new StringBuffer();
sb.append(
"[");
for (
int x =
0; x < arr.length; x++) {
if (x == arr.length -
1) {
sb.append(arr[x]);
}
else {
sb.append(arr[x]).append(
", ");
}
}
sb.append(
"]");
return sb.toString();
}
public static String arrayToString(int[] arr) {
String s =
"";
s +=
"[";
for (
int x =
0; x < arr.length; x++) {
if (x == arr.length -
1) {
s += arr[x];
}
else {
s += arr[x];
s +=
", ";
}
}
s +=
"]";
return s;
}
}
运行结果:
String字符串
7.3 把字符串反转
package cn.itcast_07;
import java.util.Scanner;
public class StringBufferTest3 {
public static void main(String[] args) {
Scanner sc =
new Scanner(System.in);
System.out.println(
"请输入数据:");
String s = sc.nextLine();
String s1 = myReverse(s);
System.out.println(
"s1:" + s1);
String s2 = myReverse2(s);
System.out.println(
"s2:" + s2);
}
public static String myReverse2(String s) {
return new StringBuffer(s).reverse().toString();
}
public static String myReverse(String s) {
String result =
"";
char[] chs = s.toCharArray();
for (
int x = chs.length -
1; x >=
0; x--) {
result += chs[x];
}
return result;
}
}
运行结果:
String字符串
7.4 判断一个字符串是否是对称字符串
package cn.itcast_07;
import java.util.Scanner;
public class StringBufferTest4 {
public static void main(String[] args) {
Scanner sc =
new Scanner(System.in);
System.out.println(
"请输入一个字符串:");
String s = sc.nextLine();
boolean b = isSame(s);
System.out.println(
"b:" + b);
boolean b2 = isSame2(s);
System.out.println(
"b2:"+b2);
}
public static boolean isSame2(String s) {
return new StringBuffer(s).reverse().toString().equals(s);
}
public static boolean isSame(String s) {
boolean flag =
true;
char[] chs = s.toCharArray();
for (
int start =
0, end = chs.length -
1; start <= end; start++, end--) {
if (chs[start] != chs[end]) {
flag =
false;
break;
}
}
return flag;
}
}
运行结果:
String字符串