Java-String类中常用的方法

涎涎原创约 1158 字大约 4 分钟...JavaJava

121-Java-String类中常用的方法.mdopen in new window

注意

本博文仅供学术研究和交流参考,严禁将其用于商业用途。如因违规使用产生的任何法律问题,使用者需自行负责。

  • String类的转换
  • String类中常用的方法
方法含义
boolean equals(String)判断两个字符串对象的内容是否相等
boolean equalsIgnoreCase(String)比较两个字符串的内容是否相等,忽略大小写
String toUpperCase( )将String对象中的所有字符都转换为大写
String toLowerCase( )将String对象中的所有字符都转换为小写
char charAt(int)返回指定索引处的 char 值
String substring(int begin)返回一个新字符串,该字符串是从begin开始的字符串的内容
String substring(int begin,int end)返回一个新字符串,该字符串是从begin开始到end-1结束的字符串的内容
int indexOf/lastIndexOf(char)返回指定字符在此字符串中第一次/最后一次出现处的索引。
int indexOf/lastIndexOf(char,int)从指定的索引开始搜索,返回在此字符串中第一次/最后一次出现指定字符处的索引
int indexOf/lastIndexOf(String)返回第一次出现的指定子字符串在此字符串中的索引
int indexOf/lastIndexOf(String,int)从指定的索引开始搜索,返回在此字符串中第一次/最后一次出现指定字符串处的索引
String trim( )返回新的字符串,忽略前导空白和尾部空白
int length( )返回此字符串的长度
String concat(String str)将指定字符串连接到此字符串的结尾
byte[] getBytes()使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
byte[] getBytes(Charset charset)使用给定的 charset将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组
String[] split(String regex)根据给定正则表达式的匹配拆分此字符串。
String replace(char oldChar, char newChar)返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的
boolean startsWith(String prefix)测试此字符串是否以指定的前缀开始
boolean endsWith(String suffix)测试此字符串是否以指定的后缀结束

示例代码:

package 字符串类;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;

public class TestAPI {

	public static void main(String[] args) {
		// charAt
		String s = new String("agddsfsdgdgdsgsdgsdj");
		
		System.out.println(s.charAt(2));//d 索引从0开始
		
		//concat()
		System.out.println(s.concat("123"));//产生一个新的字符串:s不变agddsfsdgdgdsgsdgsdj123
		
		System.out.println(s);//agddsfsdgdgdsgsdgsdj
		
		//contains()
		System.out.println(s.contains("123"));//false
		
		//需求:判断一个文件是否是图片文件,图片文件的后缀:jpg JPG png PNG gif GIF
		String fileName = "hello.gifx";
		if(!fileName.endsWith("gif") || !fileName.endsWith("GIF")) {
			System.out.println(fileName + "不是图片格式的文件");
		}
		//equalsIgnoreCase()忽略大小写比较,常用于验证码等。。。。
		System.out.println("abc".equalsIgnoreCase("ABC"));//true
		
		//getBytes() String ------> byte[]
		//byte[]: [97, 103, 100, 100, 115, 102, 115, 100, 103, 100, 103, 100, 115, 103, 115, 100, 103, 115, 100, 106]
		System.out.println("byte[]: " + Arrays.toString(s.getBytes()));
		//使用指定的字符集给String进行编码得到对应的字节数组
//		byte[]: [97, 103, 100, 100, 115, 102, 115, 100, 103, 100, 103, 100, 115, 103, 115, 100, 103, 115, 100, 106]
		System.out.println("byte[]: " + Arrays.toString(s.getBytes(Charset.defaultCharset())));
		
		
		//byte[] ----> String
		byte[] b = s.getBytes();
		String k = null;
		try {
			k = new String(b,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println("k: " + k);
		System.out.println("k.inexOf('a'): "+ k.indexOf('a'));
		
		//需求:找出字符串k中d的出现次数
		int count = 0;//存放找的次数
		int index = 0;//从哪里找
		while((index = k.indexOf('d',index + 1)) != -1) {
			System.out.println("index:" + index);
			index++;
			count++;//找到了,count + 1
		}
		System.out.println("count: " + count);
		System.out.println(k.length());
		String email = "abc@163.com.com";
		int firstIndex = email.indexOf('.');//从头开始数.最开始出现的下标
		int lastIndex = email.lastIndexOf('.');//从头开始数.最后出现的下标
		System.out.println("firstIndex: " + firstIndex);
		System.out.println("lastIndex: " + lastIndex);
		if(firstIndex != lastIndex) {
			System.out.println("邮箱格式不合法");
		}
		
		//replaceAll()
		String str = "我不爱你们,我恨你们,我恨死你们了!";
		str.replaceAll("恨","喜欢");
		System.out.println(str.replaceAll("恨","喜欢"));
		
		//split()
		String source = "a6b6afj6wou6kjs";
		String[] result = source.split("6");
		System.out.println(Arrays.toString(result));
		
		//substring()
		String a="asjfda;dsfaj;";
		System.out.println(a.substring(3));//fda;dsfaj;
		
		System.out.println("   a  b  ".trim());//只能去掉首尾的空格
	}
}

知识拓展:

JAVA正则表达式语法大全open in new window

Java正则表达式大全open in new window

史上最全常用正则表达式大全open in new window

JDK5、6、7、8新特性对比学习之简单描述open in new window

ASCII-百度百科open in new window


分割线


相关信息

以上就是我关于 Java-String类中常用的方法 知识点的整理与总结的全部内容,另附源码open in new window

上次编辑于:
贡献者: 涎涎
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.4