Java学习之:String及其方法详解

(一)String 类详解
1 实例化方式

1.1 直接赋值(用的最多)
String str = "hello";
1.2 通过构造方法赋值
String str1 = new String("hello");

2 字符串相等比较

“==”比较两个操作数的值
基本类型直接比值大小;引用数据类型比较的是堆内存地址

equals() 比较字符串内容是否相等
相等返回true,不相等返回false

3 字符串常量是 String类的匿名对象

字符串常量:使用“‘’括起来的内容。所有字符串常量都是String类的匿名对象

在进行指定字符串内容与字符串对象比较时,把字符串常量写前面,避免NullPointerExpection

public class Test1{
	public static void main(String[] args) {
		String str = null;
		System.out.println(str.equals("hello")); //NullPointerExpection
		//证明"hello"是个匿名对象
		System.out.println("hello".equals(str)); //false
	}
}
4 两种实例化模式区别:

String类的设计使用了共享设计模式
在JVM底层实际上会自动维护一个对象池(字符串常量池),如果现在采用了直接赋值的模式进行String类的对象实例化操作,那么该实例化对象(字符串内容)将自动保存到这个对象池之中。如果下次继续使用直接赋值的模式声明String类对象,此时对象池之中如若有指定内容,将直接进行引用;如若没有,则开辟新的字符串对象而后将其保存在对象池之中以供下次使用

采用构造方法:
1.采用构造方法实例化的String对象不会入池

手工入池:intern()
2.采用构造方法实例化的String对象,会开辟两块堆内存空间,其中一块成为垃圾空间

5 字符串不可变更

字符串常量不可变更

字符串的 + 操作不要出现太多次,会产生太多垃圾空间

	public static void main(String[] args) {
		String str = "hello";
		str += "World";
		str += "!!!";
		System.out.println(str); 
	}
   

可以运行,栈上的str一开始开辟的堆内存叫 hello,之后的 + 操作开辟了新的堆内存,栈上str的指向的堆内存变了,堆内存本质没变。 即字符串常量不可变更

6 字符与字符串 char’’<->String""

字符串就是字符数组
(1)将 char[] ->String
调用String类的构造方法
public String(char value[ ]):将字符数组所有内容转为String
public String(char value[ ],int offset,int count):将字符数组部分转为字符串

	public static void main(String[] args) {
		char[] data = new char[]{'h','e','l','l','o'};
		String str =new String(data);
		String str1 =new String(data,2,2);
		System.out.println(str);
		System.out.println(str1);
	}

(2)String ->char
public char charAt(int index):取得字符串指定索引的字符
public char[ ] toCharArray();

	public static void main(String[] args) {
		String str ="hello";
		char c = str.charAt(1);
		char[] data = str.toCharArray();
		System.out.println(c);//e
		System.out.println(data.length);//5
	}

判断一个字符串是否全部由数字组成?

	public static boolean isAllNumber(String str){
		//1.String -> char[]
		char[] data = str.toCharArray();
		//2.拆分出每一个字符
		for(int i = 0; i < data .length;i++) {
			char c = data[i];
			//3.判断如果数组里有一个不是数字,返回false
			if(c <'0' || c >'9'){
				return false;
			}
		}
		//4.整个for循环走完都没有发现数字,则是个纯数字字符串
		return true;
	}
7字符串与字节的转换 String <->byte

(1)字节数组 ->字符串
构造方法

(2)String ->byte
public byte[ ] getBytes(); 将字符串全部转为字节数组
public byte[ ] getBytes(String charSetName):将字符串以指定编码转为字节数组

	public static void main(String[] args) {
		String str ="哈嘿哼哈";
		byte[] data = str.getBytes();//windows 默认编码jbk
		for(byte b : data)
			System.out.println(b);
	}
8 字符串比较

d3fe208d7a1270107ab439c06284cebe.png
相等:返回0.
小于:返回内容小于0.
大于:返回内容大于0
compareTo:当碰到第一个不相等的字符时,终止比较,返回两个字符的ASCII差值

9 ***** 字符串查找*****

从完整的字符串中判断指定内容是否存在
常用:

public boolean contains(String str):判断一个子字符串是否存在
能查出的子字符串一定要连续

public int indexOf(String str):从头开始查找子字符串位置,若找到返回索引,否则返回-1

public int lastIndexOf(String str):从最后查找子字符串位置,若找到返回索引,否则返回-1
public boolean startsWith(String prefix):判断是否以指定字符串开头

public boolean endsWith(String suffix):判断是否以指定字符串结尾

	public static void main(String[] args) {
		String str ="hello";
		String str1 ="@@*hello!!";
			System.out.println(str.contains("ll"));//true
			System.out.println(str.indexOf("l"));//2
			System.out.println(str.lastIndexOf("l"));//3
			System.out.println(str1.startsWith("@"));//true
			System.out.println(str1.endsWith("!"));//true
	}
10 字符串替换

public String replaceAll(String regex,String replacement):将regex内容全部替换成replacement

	public static void main(String[] args) {
		String str ="s-b";
		str =str.replaceAll("s","*");
		str =str.replaceAll("b","*");
        System.out.println(str);//true
	}

public String replaceFirst(String regex,String replacement):替换首个内容

11 字符串拆分

将一个完整的字符串按指定格式拆分成若干个字符串
public String[ ] split(String regex): 将字符串按regex 全部拆分

	public static void main(String[] args) {
		String str ="127.0.0.1";
		//发现有些内容无法拆分开就需要使用"\"转义
		String[] result =str.split("\\.");
		for(String str1 :result)
			System.out.println(str1);
	}

若拆分结果为空,指定字符串内容需要转义\

public String[ ] split(String regex,int limit): 将字符串按照指定格式拆分为limit个字符串

12 字符串截取

public String subString(int beginIndex): 从指定索引位置开始截取到字符串结尾

public String subString(int beginIndex,int endIndex):截取部分内容
[beginIndex,endIndex) 左闭右开

首字母大写处理

	public static String upperFirstChar(String str){
		return str.subString(0,1).toUpperCase() + str.subString(1);
	}
13 其他操作方法

在这里插入图片描述
public boolean isEmpty() :返回是否是空字符串,不包含null情况

	public static void main(String[] args) {
		String str =null;
		/*NullPointerException,因为isEmpty是个普通方法,null不能调用
		if(str.isEmpty() || str == null){
			System.out.println("空字符串");
		}*/
		if(str == null || str.isEmpty()){
			System.out.println("空字符串");
		}		
	}
(二) 两个sb(StringBuffer,StringBuilder)类 ***

由于字符串常量不可变更,为了方便进行字符串内容的修改,引入sb类。

在字符串中使用“+” 进行中字符串内容拼接,会产生大量垃圾内存

引入sb类后,字符串拼接改为调用append()方法(支持各种数据类型,方法重载很多)

String 与sb的转换

String -> StringBuffer
(1)调用StringBuffer的构造方法
(2)调用append()

StringBuffer ->String
(1)调用toString()

	public static void main(String[] args) {
		String str ="test";
		// String -> StringBuffer
		StringBuffer sb = new StringBuffer(str);
		sb.append("hello").append("world");
		// StringBuffer  ->String
		System.out.println(sb.toString());
	}
解释String 与StringBuffer ,StringBuilder 的区别

https://mp.csdn.net/mdeditor#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值