对于String类的一些基本操作方法:
1.字符与字符串
- public String(char[] value) //将字符数组变为String类对象
- public String(char[] value,int offset,int count) //将部分字符数组变为String类对象
- public char charAt(int index) //返回指定索引对应的字符信息
- public char[] toCharArray() //将字符串以字符数组的形式返回
例.字符数组与字符串之间的转换
public class StringDemo {
public static void main(String args[]){
String str = "hello";
char[] data = str.toCharArray(); //将字符串变为字符数组
for (int x = 0;x<data.length;x++){
System.out.print(data[x] + " ");
}
}
}
h e l l o
例.取出指定索引的字符----使用charAt()方法
public class StringDemo {
public static void main(String args[]){
String str = "hello";
char c = str.charAt(0); //截取第一个字符
System.out.println(c); //输出字符
}
}
h
例.将字符串转为大写(利用编码值处理,即字符-32)
public class StringDemo {
public static void main(String args[]){
String str = "hello";
char[] data = str.toCharArray();
for (int x=0;x<data.length;x++){
data[x] -= 32; //改变每一个字符的编码值
}
System.out.println(new String(data)); //将全部字符数组变为String
System.out.println(new String(data,1,2)); //将部分字符数组变为String
}
}
HELLO
EL
例.给定一个字符串,要求判断其数字是否由数字组成
public class StringDemo {
public static void main(String args[]){
String str = "12345678";
if(isNumber(str)){
System.out.println("字符串由数字组成");
}
else{
System.out.println("字符好惨由非数字组成");
}
}
/**
* 判断字符串是否由数字组成
* @param temp 要判断的字符串数据
* @return 如果字符串由数字组成则返回true,否则返回false
*/
public static boolean isNumber(String temp){
char[] data = temp.toCharArray();
for (int x = 0;x<data.length;x++){
if (data[x]>'9'||data[x]<'0'){
return false;
}
}
return true;
}
}
字符串由数字组成
2.字符串的比较
- public boolean equals(String anObject) //进行相等判断,区分大小写
- public boolean equalsIgnoreCase(String anotherString) //进行相等判断,不区分大小写
- public int compareTo(String anotherString) //判断两个字符串的大小
例.判断相等
public class StringDemo {
public static void main(String args[]){
String stra = "hello";
String strb = "hELLO";
System.out.println(stra.equals(strb));
System.out.println(stra.equalsIgnoreCase(strb));
}
}
false
true
对以上两个方法的区别,即一个区分大小写,另一个则不区分大小写
例.观察compareTo()的方法
public class StringDemo {
public static void main(String args[]){
String stra = "Hello";
String strb = "HEllo";
System.out.println(stra.compareTo(strb));
if (stra.compareTo(strb)>0){
System.out.println("大于");
}
}
}
compareTo函数返回三种int型结果,>0,=0和<0
3.字符串的查找
- public boolean contains(String s) //判断指定的内容是否存在
- public int indexOf(String str,int fromIndex) //由指定位置从前向后查找指定字符串的位置
- public int lastIndexOf(String str) //从后向前查找指定字符串的位置
- public int lastIndexOf(String str,int fromIndex) //从指定位置由后向前查找字符串的位置
- public boolean startsWith(String prefix) //判断是否以指定字符串开头
- public boolean startsWith(String prefix, int toffset) //从指定位置开始判断是否以指定的字符串
- public boolean endsWith(String suffix) //判断是否以指定的字符串结尾
例.使用indexOf()等功能查找
public class StringDemo {
public static void main(String args[]){
String str = "helloworld";
System.out.println(str.indexOf("world")); //返回满足条件单词的第一个字母的索引
System.out.println(str.indexOf("l")); //返回第一个查找到的子字符串的位置
System.out.println(str.indexOf("l",5)); //从第六个元素开始查找子字符串的位置
System.out.println(str.lastIndexOf("l")); //从后向前开始查找指定字符串的位置
}
}
5
2
8
8
对index()方法也可以用来检索是否有指定的子字符串,若查询结果为-1,则代表子字符串不存在
例.使用contains()方法判断子字符串是否存在
public class StringDemo {
public static void main(String args[]){
String str = "helloworld";
if (str.contains("world")){
System.out.println("可以查询到数据");
}
}
}
可以查询到数据
例.开头和结尾的判断
public class StringDemo {
public static void main(String args[]){
String str = "##@@hello**";
System.out.println(str.startsWith("##")); //是否以##开头
System.out.println(str.startsWith("@@",2)); //从第二个开始索引是否以@@开头
System.out.println(str.endsWith("**")); //是否以**结尾
}
}
true
true
true
4.字符串的替换
- public String replaceAll(String regex,String replacement) //用新的内容替换全部旧的内容
- public String replaceFirst(String regex,String replacement) //替换首个满足条件的内容
例.观察替换的结果
public class StringDemo {
public static void main(String args[]){
String str = "helloworld";
String resultA = str.replaceAll("l", "_"); //全部替换
String resultB = str.replaceFirst("l", "_"); //替换首个
System.out.println(resultA);
System.out.println(resultB);
}
}
he__owor_d
he_loworld
5.字符串的截取
- public String substring(int beginIndex) //从指定索引截取到结尾
- public String substring(int beginIndex,int endIndex) //截取部分子字符串的数据
例.验证操作
public class StringDemo {
public static void main(String args[]){
String str = "helloworld";
String resultA = str.substring(5); //从指定索引截取到结尾
String resultB = str.substring(0,5); //截取部分子字符串
System.out.println(resultA);
System.out.println(resultB);
}
}
world
hello
6.字符串的拆分
- public String[] split(String regex) //按照指定的字符串进行全部拆分
- public String[] split(String regex,int limit) //按照指定的字符串进行部分拆分,最后的数组长度由limit决定
例.进行全部拆分
public class StringDemo {
public static void main(String args[]){
String str = "hello yootk nihao mldn"; //定义字符串,用空格作为间隔
String result[] = str.split(" "); //字符串拆分
for (int x=0;x<result.length;x++){
System.out.print(result[x] + "、");
}
}
}
hello、yootk、nihao、mldn、
注:如果split方法中设置的是一个空字符串,即"",那么就表示全部拆分,即将整个字符串变为一个字符串数组,而数组的长度就是字符串的长度
例.拆分为指定的个数
public class StringDemo {
public static void main(String args[]){
String str = "hello yootk nihao mldn"; //定义字符串,用空格作为间隔
String result[] = str.split(" ",2); //字符串拆分
for (int x=0;x<result.length;x++){
System.out.println(result[x]);
}
}
}
hello
yootk nihao mldn
其中2代表将字符串拆分成两个长度的字符串对象数组
例.复杂拆分
public class StringDemo {
public static void main(String args[]){
String str = "张三:20|李四:21|王五:22";
String result[] = str.split("\\|"); //第一次拆分
for (int x=0;x<result.length;x++){
String temp[] = result[x].split(":"); //第二次拆分
System.out.println("姓名: " + temp[0] + "; 年龄: " + temp[1]);
}
}
}
姓名: 张三; 年龄: 20
姓名: 李四; 年龄: 21
姓名: 王五; 年龄: 22
7.其他方法
- public String contact(String str) //字符串连接,和“+”效果一致
- public String toLowerCase() //转小写
- public String toUpperCase() //转大写
- public String trim() //去掉字符串左右两边的空格,中间空格保留
- public int length() //取得字符串长度
- public String intern() //数据入池
- public boolean isEmpty() //判断是否是空字符串
例.字符串连接
public class StringDemo {
public static void main(String args[]){
String str = "hello".concat("world");
System.out.println(str);
}
}
helloworld
例.转小写与转大写操作
public class StringDemo {
public static void main(String args[]){
String str = "(*(*Hello(*(*";
System.out.println(str.toUpperCase()); //转大写输出
System.out.println(str.toLowerCase()); //转小写输出
}
}
(*(*HELLO(*(*
(*(*hello(*(*
例.去掉左右空格
public class StringDemo {
public static void main(String args[]){
String str = " hello world";
System.out.println("【" + str + "】"); //原始字符串
System.out.println("【" + str.trim() + "】"); //去掉空格后的字符串
}
}
【 hello world】
【hello world】
注:若想去除中间的空格,可以利用方法replaceAll()方法将空格替换为空字符串
例.实现首字母大写的操作
public class StringDemo {
public static void main(String args[]){
String str = "yootk";
System.out.println(initcap(str));
}
/**
* 实现首字母大写的操作
* @param temp要转换的字符串数据
* @return 将首字母大写后返回
*/
public static String initcap(String temp){
//先利用substring(0,1)取出字符串的第一位后将其变为大写,再连接剩余的字符串
return temp.substring(0,1).toUpperCase() + temp.substring(1);
}
}
Yootk