Java中关于String下面的方法

本文介绍了Java中的String类的一些核心方法,包括获取字符串长度、获取特定位置字符、查找字符下标、判断字符串状态以及字符串转换。此外,还展示了字符替换、字符串切割、截取子串、大小写转换和去除空格的功能。
摘要由CSDN通过智能技术生成
1.4String类下面的方法

获取字符串长度 int length();

获取特定位置的字符 char charAt(int index);

获取特定字符的下标 int indexOf(String str);

获取特定字符的最后的一个位置 int lastIndexOf(int ch);

package com.qf.a_string;

public class Demo3 {
	public static void main(String[] args) {
		String str1 =  "中国梦";
		System.out.println(str1.length());//6
		
		String str2 = "abcdef";
		System.out.println(str2.charAt(3));//找str2字符串中下标为 0位置的上的字符
		
		
		String str3 = "abcdecvf";
		
		System.out.println(str3.indexOf("cd"));//获取的是字符的下标
		System.out.println(str3.indexOf(98));//1
		System.out.println(str3.indexOf('c'));//2
		System.out.println(str3.lastIndexOf('c'));//5
		
		
		
		
	}
}

返回值是布尔类型数据

boolean endWith(String str); 是否以指定的字符或者字符串结尾

boolean isEmpty();判断是否为空,如果字符串为空就 true

boolean contains(); 判断是否包含子字符串

boolean equals(Object anOnject); 判断两个字符串是否相等

boolean equalsIgoreCase(Object anObject); 忽略大小写判断两个字符串是否相等

package com.qf.a_string;

public class Demo4 {
	public static void main(String[] args) {
		
		String str1 = "Demo1.java";
		System.out.println(str1.endsWith(".java"));//true
		System.out.println(str1.endsWith("av"));//false
		System.out.println(str1.endsWith("va"));//true
		
		
		String str2 = "";
		System.out.println(str2.isEmpty());//true
		String str3 = " ";
		System.out.println(str3.isEmpty());//flase  不为空 空格也是有内容的
		
		
		String str4 = "abcdef";
		System.out.println(str4.contains("cd"));//true
		System.out.println(str4.contains("ce"));//false
		System.out.println(str4.contains("abcde"));//true
		
		System.out.println(str4.equals("abcdeF"));//false
		
		System.out.println(str4.equalsIgnoreCase("abcDeF"));//true
		
		
	}
}

将字符数组转为字符串

直接可以使用STring类的构造方法

String(char[] value)

String(char[] value, int offset, int count)

static String valueOf(char[] chs);

将字符串转为字符数组

char[] toCharArray();

package com.qf.a_string;

public class Demo5 {
	public static void main(String[] args) {
		
		//将字符数组转为字符串
		char[] chs = {'a', 'b', 'c', 'd', 'e'};
		String str = new String(chs);
		System.out.println(str);//abc
		//String(char[] chs, int offset, int count);  
		//第二个参数  int  offset  偏移量 从第几个开始
		//第三个参数 int  count   个数 数量
		String str1 = new String(chs, 4, 1);
		System.out.println(str1);
		
		String str2 = String.valueOf(chs);
		System.out.println(str2);
		
		
		//将字符串转为字符数组
		char[] arr1 = "狗蛋很狗".toCharArray();
		for (int i = 0; i < arr1.length; i++) {
			System.out.println(arr1[i]);
		}
		
	}
}	

以下是几个比较重要的方法

String replace(char oldChar, char newChar); 字符在字符串中替换

String [] split(String regex); 以 regex 对当前的字符串进行切割

String subString(int beginIndex); 截取字符串一部分

String subString(int beginIndex, int endIndex); 截取字符串一部分

String toUpperCase();将小写字母转为大写的字母

String toLowerCase();将大写转为小写字母

String trim(); 去除左右空格

package com.qf.a_string;

import java.util.Arrays;

public class Demo6 {
	public static void main(String[] args) {
		
		String str = "abcdef";
		System.out.println(str.replace('c', '中'));
		System.out.println(str.replace("cd", "牛彩云"));
		
		
		String str1 = "嘻嘻哒呵呵哒么么哒哈哈哒";
		//以哒切割
		String[] strs = str1.split("哒");
		System.out.println(strs);
		System.out.println(Arrays.toString(strs));//[嘻嘻, 呵呵, 么么, 哈哈]
		
		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);
		}
		
		String str2 = "ab,cd,ef,g";
		//以,  切割
		String[] strs1 = str2.split(",");
		System.out.println(Arrays.toString(strs1));
		
		
		//截取一部分的字符串
		String str3 = "abcdef";
		System.out.println(str3.substring(2));//cdef
		
		
		String str4 = "大学先把手机农村";
		System.out.println(str4.substring(2, 4));// 先把//要头不要尾
		
		
		
		String str5 = "abCDF";
		System.out.println(str5.toUpperCase());//ABCDF
		System.out.println(str5.toLowerCase());//abcdf
		
		
		String str6 = "    index    xixi   ";
		System.out.println(str6);
		System.out.println(str6.trim());
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值