Java中的字符串也是一连串的字符,但是Java将字符串作为String类型对象来处理
1、String构造函数
- 创建空String的构造函数称为默认构造函数
String s = new String();
- 用如下的构造函数可以创建一个被字符数组初始化的字符串
String s = new String(char chars[]);
- 使用下面的构造函数可以指定字符数组的一个子区域作为初始化值
String s = new String(char chars[], int startIndex, int numChars);
- 用下面的构造函数可以构造一个String对象,该对象包括了与另一个String对象相同的字符序列。
String s = new String(String strObj);
2、字符串长度
int length()
3、特殊的字符串操作
1)字符串文字
String s = "abc";
2)字符串连接
可以使用运算符+连接字符串
3)和其他类型的数据连接
自动转为String类型
4)字符串转换和toString()
当Java 在连接时将数据转换为其字符串形式时,它是通过调用一个由字符(String)定义的字符串转换方法valueOf( )的重载来完成的。
4、字符截取
String类提供了许多从String对象中截取字符的方法
1)charAt()
从字符串中截取一个字符
charAt(int where)
- public static void main(String[] args) {
- String s="abcde";
- System.out.println(s.charAt(1));
- }
- //代码运行输出为b 说明下标从0开始
2)getChars()
如果一次想截取多个字符,可以使用getChars()
- void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)
这里sourceStart 指定了子字符串开始的下标, sourceEnd指定了子字符串结束的下一个字符的下标。 因此子字符串包含了从sourceStart 到sourceEnd–1的字符。 获得字符的数组由target所指定。 |
- public static void main(String[] args) {
- String s="abcdefg";
- int start = 1;
- int end =3;
- char buf[] = new char[end - start];
- s.getChars(start, end, buf, 0);
- System.out.println(buf);
- }
- //结果输出为 bc
3)getBytes()
有一种称为getBytes( )的方法,它是实现将字符存放于字节数组中的getChars( )方法的替代,它使用平台提供的默认的字符到字节的转换。
4)tocharArray()
如果想将字符串(String)对象中的字符转换为一个字符数组,最简单的方法就是调用toCharArray( ) 方法。对应整个字符串,它返回一个字符数组
5、字符串比较
1)equals()和equalsIgnoreCase()
使用equals( ) 方法比较两个字符串是否相等
- boolean equals(Object str)
为了执行忽略大小写的比较,可以调用equalsIgnoreCase( ) 方法
- boolean equalsIgnoreCase(String str)
2)regionMatches()
regionMatches( ) 方法将一个字符串中指定的区间和另一字符串中指定的区间进行比较。它的重载形式允许在比较时忽略大小写。
- boolean regionMatches(int startIndex, String str2, int str2StartIndex, int numChars)
- // ignoreCase是否区分大小写
- boolean regionMatches(boolean ignoreCase,int startIndex, String str2,int str2StartIndex, int numChars)
接下来看下这个方法的使用
- public static void main(String[] args) {
- String s1 = "abcdefg";
- String s2 = "bcdefgh";
- String s3 = "hijklmn";
- //s1从下标1开始截取3个字符串 和 s2从下标0开始截取3个字符串 相比较
- System.out.println(s1.regionMatches(1, s2, 0, 3));
- //s1从下标1开始截取3个字符串 和 s3从下标1开始截取3个字符串 相比较
- System.out.println(s1.regionMatches(1, s3, 1, 3));
- }
- //第一行输出true 第二行输出false
3)startsWith()和endsWith()
startsWith( ) 方法判断一个给定的字符串(String)是否从一个指定的字符串开始
endsWith( ) 方法判断所讨论的字符串(String)是否是以一个指定的字符串结尾
- boolean startsWith(String str)
- boolean endsWith(String str)
例子:
- public static void main(String[] args) {
- String s="abcdefgh";
- System.out.println("s为: "+s);
- System.out.println("s是否是ab开头"+s.startsWith("ab"));
- System.out.println("s是否是gh开头"+s.endsWith("gh"));
- System.out.println("s是否是bc开头"+s.startsWith("bc"));
- System.out.println("s是否是hi开头"+s.endsWith("hi"));
- }
- // 输出内容
- // ↓↓↓
- // s为: abcdefgh
- // s是否是ab开头: true
- // s是否是gh开头: true
- // s是否是bc开头: false
- // s是否是hi开头: false
4) equals()与==的比较
equals( ) 方法比较字符串(String)对象中的字符
==运算符比较两个对象引用看它们是否引用相同的实例
- public static void main(String[] args) {
- String s1 = "abc";
- String s2 = "abc";
- System.out.println("equals结果:\t"+s1.equals(s2));
- System.out.println("==结果:\t"+(s1==s2));
- }
- // 输出结果
- // ↓↓↓
- // equals结果: true
- // ==结果: true
5)compareTo()
一个字符串小于另一个指的是它在字典中先出现,而一个字符串大于另一个指的是它在字典中后出现。字符串(String)的compareTo( )方法实现了这种功能
- 小于0 调用字符串小于str
- 大于0 调用字符串大于str
- 等于0 两个字符串相等
例子:
- public static void main(String[] args) {
- String arr[] = { "Now", "is", "the", "time", "for", "all", "good",
- "men", "to", "come", "to", "the", "aid", "of", "their",
- "country" };
- for (int i = 0; i < arr.length; i++) {
- for (int j = i + 1; j < arr.length; j++) {
- if(arr[i].compareTo(arr[j])>0){
- String temp = arr[i];
- arr[i]=arr[j];
- arr[j]=temp;
- }
- }
- }
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i]+" ");
- }
- }
- //输出为:Now aid all come country for good is men of the the their time to to
- //由于Now的N为大写 所以排在最前面
如果需要忽略大小写 则用compareToIgnoreCase()
6、搜索字符串
String类提供了两个方法,允许在字符串中搜索指定的字符或子字符串:
方法返回字符或子字符串被发现的位置的下标,当搜索失败时,返回-1。
-
indexOf() 搜索字符或子字符串首次出现。
-
lastIndexOf() 搜索字符或子字符串的最后一次出现。
indexOf(int ch)
indexOf(String str)
indexOf(int ch, int fromIndex)
indexOf(String str, int fromIndex)
lastIndexOf(int ch)
lastIndexOf(String str)
lastIndexOf(int ch, int fromIndex)
lastIndexOf(String str, int fromIndex)
7、修改字符串
1)substring()
截取字符串
- //substring 截取字符串
- //返回从下标startIndex到字符串结尾的字符串
- String substring(int startIndex)
- //返回从startIndex到endIndex-1的字符串
- String substring(int startIndex, int endIndex)
2)concat()
连接两个字符串
String concat(String str)
- public static void main(String[] args) {
- String s = "abcdefg";
- String s2 = "hijk";
- System.out.println(s.concat(s2));
- }
- // 输出内容
- // ↓↓↓
- // abcdefghijk
3)replace()
replace( ) 方法用另一个字符代替调用字符串中一个字符的所有具体值
String replace(char original, char replacement)
例子:
- public static void main(String[] args) {
- String s = "akljfadja";
- System.out.println(s.replace('a', 'b'));
- }
- // 输出结果
- // ↓↓↓
- // bkljfbdjb
- // 将字符串中的字符a全部替换为字符b
4)trim()
trim( ) 方法返回一个调用字符串的拷贝,该字符串是将位于调用字符串前面和后面的空白符删除后的剩余部分
8、利用valueOf( )方法实现数据转换
- static String valueOf(double num)
- static String valueOf(long num)
- static String valueOf(Object ob)
- static String valueOf(char chars[])
9、改变字符串内字符的大小写
- toLowerCase( ) 方法将字符串内的所有字符从大写字母改写为小写字母
- toUpperCase( )方法将字符串内所有字符从小写字母改写为大写字母
例子:
- public static void main(String[] args) {
- String s = "adsGFdasGrSa";
- System.out.println(s.toLowerCase());
- System.out.println(s.toUpperCase());
- }
- // 输出结果
- // ↓↓↓
- // adsgfdasgrsa
- // ADSGFDASGRSA
转载于:https://blog.51cto.com/gejw0623/1117982