目录
String概述
简介:String是java常用类之一,位于java.lang.包下同时也是字符串类,该类其中包含了五十多种方法,其中有很多方法都较为常用。
可通过java官方JDK文档在线查询:Overview (Java Platform SE 8 ) (oracle.com)
JDK帮助文档官方下载地址: Java 开发工具包 8 文档 (oracle.com)
官方说明:
String
类代表字符串。 Java程序中的所有字符串文字(例如"abc"
)都被实现为此类的实例。
String常用方法:
方法签名称 | 方法作用描述 |
int length() | 该方法用于获取字符串长度 |
String trim() | 该方法用于去除字符串两端首尾空格,或者空白 |
boolean startsWith(String prefix) | 该方法用来判断是否以指定内容开头 |
boolean endsWith(String suffix) | 该方法用来判断是否以指定内容结尾 |
boolean contains(CharSequence s) | 该方法用来判断当前字符串是否包含指定的字符串 |
String toUpperCase() | 该方法用来将字符串全部转换为大写,如果原本是大写则字符串不变 |
String toLowerCase() | 该方法用来将字符串全部转换为小写,如果原本是小写则字符串不变 |
String substring(int start) | 该方法用来截取字符串截取从start后面的所有字符串 |
String substring(int start,int end) | 该方法用来截取指定位置字符从start索引开始到end结束(包头不包尾) |
char[] toCharArray() | 该方法将字符串变为Char类型的数组 |
char charAt(int index) | 该方法返回index下标处的指定字符 |
int indexOf(String ch) | 该方法用来查找字符串中的指定字符下标,查找到则返回对应字符下标否则返回-1 |
int indexOf(String ch,int xx) | 该方法从xx下标处开始查找指定字符下标,查找到则返回对应字符下标否则返回-1 |
String的三种定义方式:
package com.text;
public class test51 {
public static void main(String[] args) {
//第一种定义方式 通过""直接创建(常用方式)
String a="Hello world";
//第二种定义方式 表示定义了String类但没有创建对象
String b=null;
//第三种 通过new创建对象
String c=new String("Hello world");
}
}
String对比:
package com.text;
/**
* 测试字符串对比,
* 使用==表示是判断字符串地址是否相同
* 使用equals()判断表示值是否相同
*/
public class test51 {
public static void main(String[] args) {
//第一种对比方式
String a="b";
String b=new String("b");
System.out.println(a==b); //false 因为==判断地址是否相同
System.out.println(a.equals(b)); //true 因为.equals()判断两个字符串的值是否相同
//第二种对比方式
String a1=new String("a");
String a2=new String("a");
System.out.println(a1==a2);//false
System.out.println(a1.equals(a2));//true
//第三种方式
String b1="a";
String b2="a";
System.out.println(b1==b2); //true 因为他们的地址相同,因为b2字符串引用了b1字符串值的地址(这里不懂的问老师这里是重点!!!)
System.out.println(b1.equals(b2));//true
}
}
常用方法测试:
package com.text;
import java.util.Arrays;
/**
* 测试字符串常用方法
*/
@SuppressWarnings("all")
public class test52 {
public static void main(String[] args) {
//定义字符串
String a="Hello world ";
//1.int length() 用法返回int类型的字符串长度
int length = a.length();
System.out.println("a字符串长度为-->"+length);
//2.String trim() 用法该方法用来去掉首尾空格,会返回一个新的字符串并不会修改原来的字符串
String trim = a.trim();
System.out.println("未去掉首尾空格的字符串-->"+a);
System.out.println("去掉首尾空格的字符串-->"+trim);
//3.boolean startsWith(String prefix) 判断字符串是否由指定内容开头,不包括大小写
boolean h = a.startsWith("H");
System.out.println(h);
//4.boolean endsWith(String suffix) 判断字符串是否以指定内容结尾,不包括大小写
boolean d = a.endsWith("d ");
System.out.println(d);
//5.boolean contains(CharSequence s) 判断字符串是否包含
boolean world = a.contains("world");
System.out.println(world);
//6:String toUpperCase() 将字符串全部转换为大写
String s = a.toUpperCase();
System.out.println("将字符串全部转换为大写-->"+s);
//7.String toLowerCase() 将字符串全部转换为小写
String s1 = s.toLowerCase();
System.out.println("将字符串全部转换为小写-->"+s1);
//8.String substring(int start) 该方法用来截取字符串截取从start后面的所有字符串
String substring = a.substring(6);
System.out.println(substring);
//9.String substring(int start,int end) 该方法用来截取指定位置字符串操你个start索引开始到end结束(包头不包尾)
String substring1 = a.substring(6, 11);
System.out.println(substring1);
//10.char[] toCharArray() 该方法将字符串变为Char类型的数组(暂时不用管)
char[] chars = a.toCharArray();
System.out.println(Arrays.toString(chars));
//11.char charAt(int index) 该方法用来查找字符串中的指定字符下标,查找到则返回对应字符下标否则返回-1
char c = a.charAt(0);
System.out.println(c);
//12.int indexOf(String ch) 该方法用来查找字符串中的指定字符下标,查找到则返回对应字符下标否则返回-1
int index = a.indexOf("H");
System.out.println(index);
//13.int indexOf(String ch,int xx) 该方法从xx下标处开始查找指定字符下标,查找到则返回对应字符下标否则返回-1
int index1 = a.indexOf("H", 0);
System.out.println(index1);
}
}
小知识:String的特点是一个不可变的字符串.