-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
一、String类的概述
String
类代表字符串。Java 程序中的所有字符串字面值(如"abc"
)都作为此类的实例实现。
二、String类的构造方法
public String():空构造
public String(byte[] bytes):把字节数组转成字符串
public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
public String(char[] value):把字符数组转成字符串
public String(char[] value,int index,int count):把字符数组的一部分转成字符串
public String(String original):把字符串常量值转成字符串
package cn.hebei.sjz_String;
/*
* String类的构造方法
*/
public class Demo {
public static void main(String[] args) {
// public String():空构造
String s = new String();
System.out.println("s= " + s);// s=
// 返回此字符串的长度
System.out.println("s.length =" + s.length());// s.length =0
// public String(byte[],bytes) 将字符数组转换为字符串
byte[] by = { 97, 98, 99, 100 };
String s1 = new String(by);
System.out.println(s1);// abcd
System.out.println(s1.length());// 4
// public String(byte[],bytes,int index,int length)把字节数组的一部分转换成字符串
String s2 = new String(by, 1, 2);
System.out.println(s2);// bc
// public String(char[],value)把字符数组转换成字符串
char[] ch = { 'a', 'b', 'c', 'd', 'w' };
String s3 = new String(ch);
System.out.println(s3);// abcdw
// public String(char[],value,int index,int count)把字符数组的一部分转换成字符串
String s4 = new String(ch, 1, 3);
System.out.println(s4);// bcd
// public String(String original) 把字符串常量值转换成字符串
String s5 = new String("hello world");
System.out.println(s5);// hello world
String s6 = "hello world";
System.out.println(s6);// hello world
}
}
三、String类的判断功能
boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
boolean contains(String str):判断大字符串中是否包含小字符串
boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
boolean isEmpty():判断字符串是否为空。
package cn.hebei.sjz_String;
/*
* String类的判断功能
*/
public class Demo1 {
public static void main(String[] args) {
String s = "hello";
String s1 = "HELLO";
String s2 = "he";
// 判断两个字符串内容是否相同,区分大小写
System.out.println(s.equals(s1));// false
System.out.println(s.equals(s2));// fales
// 判断两个字符串内容是否相同,不区分大小写
System.out.println(s.equalsIgnoreCase(s1));// true
System.out.println(s.equalsIgnoreCase(s2));// false
// 判断大字符串是否包含小字符串
System.out.println(s.contains(s2));// true
System.out.println(s.contains(s1));// false
// 判断字符串是否以“he”开头
System.out.println(s.startsWith("he"));// true
// 判断字符串是否以”lo“结尾
System.out.println(s.endsWith("lo"));// true
// 判断字符串是否为空
System.out.println(s.isEmpty());// false
}
}
练习:
package cn.hebei.sjz_String;
import java.util.Scanner;
/*
* A:案例演示
* 需求:模拟登录,给三次机会,并提示还有几次。
* B:断点查看
* 模拟用户登录代码
*/
public class Test {
public static void main(String[] args) {
String passName = "dengluming";
String passWord = "mima";
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入账号");
String name = sc.next();
System.out.println("请输入密码");
String word = sc.next();
if (name.equals(passName) && word.equals(passWord)) {
System.out.println("登陆成功");
} else {
if (i == 2) {
System.out.println("该账户已被冻结,请到柜台办理");
break;
}
System.out.println("登陆失败,请重新登陆,还剩" + (2 - i) + "次机会");
}
}
}
}
四、String类的获取功能
int length():获取字符串的长度。
char charAt(int index):获取指定索引位置的字符
int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
String substring(int start):从指定位置开始截取字符串,默认到末尾。
String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。包左不包右。
package cn.hebei.sjz_String;
/*
* String类的获取功能
*/
public class Demo2 {
public static void main(String[] args) {
String s = "abcdefghiadfsdefsd";
// 获取字符串长度
System.out.println(s.length());// 18
// 获取指定索引位置的字符
System.out.println(s.charAt(2));// c
// 返回指定字符在字符串中第一次出现的索引
System.out.println(s.indexOf('d'));// 3
// 返回指定字符串在字符串中第一次出现的索引
System.out.println(s.indexOf("bc"));// 1
// 返回指定字符在字符串中从指定位置开始后第一次出现的索引
System.out.println(s.indexOf('d', 3));// 3
System.out.println(s.indexOf('d', 4));// 10
// 返回指定字符串在字符串中从指定位置开始后第一次出现的索引
System.out.println(s.indexOf("de", 4));// 13
// 从指定位置截取字符串
System.out.println(s.substring(2));// cdefghiadfsdefsd
System.out.println(s.substring(2, 5));// cde
}
}
练习:
package cn.hebei.sjz_String;
/*
* 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
*
* "abc2334SFkdjfkDIJN888900"
* 遍历字符串,然后判断是否为大写,数字,小写
* 大写 A-Z 小写 a-z 数字 0-9
*/
public class Test1 {
public static void main(String[] args) {
String s = "abc2334SFkdjfkDIJN888900";
int num1 = 0;
int num2 = 0;
int num3 = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) <= 'Z' && s.charAt(i) >= 'A') {
num1++;
} else if (s.charAt(i) <= 'z' && s.charAt(i) >= 'a') {
num2++;
} else {
num3++;
}
}
System.out.println("大写字母有" + num1);//6
System.out.println("小写字母有" + num2);//8
System.out.println("数字有" + num3);//10
}
}
五、String类的转换功能
byte[] getBytes():把字符串转换为字节数组。
char[] toCharArray():把字符串转换为字符数组。
static String valueOf(char[] chs):把字符数组转成字符串。
static String valueOf(int i):把int类型的数据转成字符串。
String toLowerCase():把字符串转成小写。
String toUpperCase():把字符串转成大写。
String concat(String str):把字符串拼接。
package cn.hebei.sjz_String;
/*
* String类的转换功能
*/
public class Demo3 {
public static void main(String[] args) {
String s = "abcdef";
// 字符串转换成字节数组
byte[] by = s.getBytes();
for (int i = 0; i < by.length; i++) {
System.out.print(by[i] + ",");// 97,98,99,100,101,102,
}
System.out.println();
// 字符串转换成字符数组
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + ",");// a,b,c,d,e,f,
}
System.out.println();
// 字符数组转换成字符串
String valueOf = String.valueOf(ch);
System.out.println(valueOf);// abcdef
// 把int类型的数据转换成字符串
String valueOf2 = String.valueOf(98);
System.out.println(valueOf2);// 98
// 转换为小写
System.out.println(s.toLowerCase());// abcdef
System.out.println(s);// abcdef
// 转换为大写
System.out.println(s.toUpperCase());// ABCDEF
System.out.println(s);// abcdef
// 字符串拼接
String s1 = "abc";
String s2 = "cde";
System.out.println(s1.concat(s2));// abccde
}
}
练习:
package cn.hebei.sjz_String;
/*
* 把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
*/
public class Test2 {
public static void main(String[] args) {
String s = "adhcJKDSFadfk";
// 截取s中的第一个字符成一个新的字符串,截取从索引1开始组成新的字符串
String s1 = s.substring(0, 1);
String s2 = s.substring(1);
// 拼接字符串
String s3 = s1.toUpperCase() + s2.toLowerCase();
System.out.println(s3);
}
}
六、String类的其他功能
A:String的替换功能
String replace(char old,char new)
String replace(String old,String new)
package cn.hebei.sjz_String;
/*
* String类的替换功能
*/
public class Demo4 {
public static void main(String[] args) {
String s = "zheshism";
// 替换字符
System.out.println(s.replace('e', 'i'));// zhishism
System.out.println(s);// zheshism 没有改变原来的字符串
// 替换字符串
System.out.println(s.replaceAll("shi", "cai"));// zhecaism
System.out.println(s);// zheshism 没有改变原来的字符串
// 原字符串中没有要替换的字符串,则不执行,还是原来的字符串
System.out.println(s.replaceAll("women", "ciakk"));// zheshism
}
}
B:String的去除字符串两空格
String trim()
package cn.hebei.sjz_String;
/*
* String类的去除字符串两边的空格功能
*/
public class Demo4 {
public static void main(String[] args) {
//去除两边空格
String s1 = " java String ";
String s2 = " EE String ";
System.out.println(s1.trim()+"----");//java String----
System.out.println(s2+"-----");//java String----
}
}
C:String的按字典顺序比较两个字符串
int compareTo(String str)
int compareToIgnoreCase(String str)
package cn.hebei.sjz_String;
/*
* String类的按字典顺序排序功能
*/
public class Demo4 {
public static void main(String[] args) {
// 字典顺序排序
String s = "hehe";
String s1 = "woshi";
String s2 = "Hehe";
String s3 = "woshI";
String s4 = "woshi";
System.out.println(s.compareTo(s1));// -15
System.out.println(s.compareTo(s2));// 32
System.out.println(s1.compareTo(s4));// 0
System.out.println(s1.compareTo(s3));// 32
}
}
package cn.hebei.sjz_String;
/*
* 把数组中的数据按照指定个格式拼接成一个字符串,输出结果为"[1, 2, 3]"
*/
public class Test3 {
public static void main(String[] args) {
int[] intArray = { 1, 2, 3 };
// 定义一个用于接收的空字符串
String s = "";
// 拼接
s += "[";
// 遍历数组
for (int i = 0; i < intArray.length; i++) {
if (i == 2) {
s += intArray[i];
s += "]";
} else {
s += intArray[i];
s += ",";
}
}
System.out.println(s);
}
}
package cn.hebei.sjz_String;
/*
* 字符串反转并断点查看
* A:案例演示
* 需求:把字符串反转
* 举例:键盘录入"abc"
* 输出结果:"cba"
*/
public class Test4 {
public static void main(String[] args) {
String s = "abcdef";
System.out.println(reverse(s));
}
public static String reverse(String s) {
// 接收字符串
String s1 = "";
// 将字符串转换成数组
char[] ch = s.toCharArray();
// 逆序遍历
for (int i = ch.length -1; i >= 0; i--) {
s1 += ch[i];
}
return s1;
}
}