Java学习笔记第五天——String类和StringBuilder类

Scanner 类:
用于获取键盘录入的数据。(基本数据类型,字符串数据)
Scanner sc = new Scanner(system.in);

String:字符串类
由多个字符组成的一串数据
字符串其本质就是一个字符数组

构造方法:
String(String original):把字符串数据封装成字符串对象
String(char[] value):把字符数组的数据封装成字符串对象
String(char[] value,int index,int count):把字符数组中的一部分数据封装成字符串对象
//把数组从index索引开始到第count的所有数据封装成字符串对象
注意:字符串是一种比较特殊的引用数据类型,直接输出字符串对象最后输出的是对象中的数据
eg:

public class StringDemo {
public static void main(String[] args) {
	//方式1
	//String(String original):把字符串数据封装成字符串对象
	String s = new String("hello");
	System.out.println("s:"+s);
	System.out.println("----------");
	
	//方式2
	//String(char[] value):把字符数组的数据封装成字符串对象
	char[] arr ={'h','e','l','l','o'};
	String s2 = new String(arr);
	System.out.println("s2:"+s2);
	System.out.println("----------");
	
	//方式3
	//String(cahr[] value,int index,int count):把字符数组中的一部分数据封装成字符串对象
	String s3 = new String(arr,0,arr.length);
	System.out.println("s3:"+s3);
	
	
}
}

通过构造方法创建的字符串对象和直接赋值方式创建的字符串对象的区别

  • 通过构造方法创建字符串对象是在堆内存
  • 直接赋值方式创建对象是在方法区的常量池

字符串的内容是存储在方法区的常量池里面的,是为了方便字符串的重复使用

==判断的是什么:

  1. 基本数据类型:比较的是基本数据类型的值是否相同
  2. 引用数据类型:比较的是引用数据类型的地址值是否相同

eg:

public class StringDemo2 {
public static void main(String[] args) {
	String s1 = new String("hello");
	String s2 = "hello";
	String s3 = "hello";
	System.out.println("s1:"+s1);//hello
	System.out.println("s2:"+s2);//hello
	
	System.out.println("s1==s2:"+(s1==s2));//false	
	
	System.out.println("s1==s3:"+(s1==s3));//false
	System.out.println("s3==s2:"+(s3==s2));//true
}
}

Object类:
是类层次结构中的根类,那么所有的类都直接或间接的继承自该类
如果一个方法的形式参数是Object,那么这里我们就可以传递他的任意子类对象

String类的判断功能:

  1. boolean equals(Object obj):比较字符串的内容是否相同(区分大小写)
  2. boolean equalsIgnoreCase(String str):比较字符串的内容是否相同(忽略大小写)
  3. boolean startWith(String str):判断字符串对象是否以指定的str开头(区分大小写)
  4. boolean endWith(String str):判断字符串对象是否以指定的str结尾(区分大小写)

eg:

public class StringDemo {
    public static void main(String[] args) {
    	String s1 = "hello";
    	String s2 = "hello";
    	String s3 = "Hello";
   
    	

//boolean equals(Object obj):比较字符串的内容是否相同(区分大小写)
        	System.out.println(s1.equals("hello"));
        	System.out.println(s1.equals(s3));
        	System.out.println("-------------");
        	
   

//boolean equalsIgnoreCase(String str):比较字符串的内容是否相同(忽略大小写)
    	System.out.println(s1.equalsIgnoreCase(s2));
    	System.out.println(s1.equalsIgnoreCase(s3));
    	System.out.println("-------------");
    	
    	

//boolean startWith(String str):判断字符串对象是否以指定的str开头
        	System.out.println(s1.startsWith("He"));
        	System.out.println(s1.startsWith("he"));
        	System.out.println("-------------");
        	
        	

//boolean endsWith(String str):判断字符串对象是否以指定的str结尾(区分大小写)
            	System.out.println(s1.endsWith("lo"));
            	System.out.println(s1.endsWith("ol"));
            }
            }

String类的获取功能:

  1. int length():获取字符串的长度,其实也就是字符的个数。
  2. char cahrAt(int index):获取指定索引处的字符
  3. int indexOf(String str):获取str在字符对象中第一次出现的索引(str的第一个字符的索引)
  4. String substring(int start):从start开始截取字符串,包括start
  5. String substring(int start,int end):从start开始,到end结束截取字符串。包括start,不包括end

eg:

public class StringDemo {
public static void main(String[] args) {
	//创建字符串对象
			String s = "helloworld";
			
			//int length():获取字符串的长度,其实也就是字符的个数。
			System.out.println(s.length());
			System.out.println("------------");
			
			//char charAt(int index):获取指定索引处的字符。
			System.out.println(s.charAt(3));
			System.out.println("------------");
			
			//int indexOf(String str):获取str在字符处对象中第一次出现的索引
			System.out.println(s.indexOf("l"));
			System.out.println(s.indexOf("owo"));
			System.out.println(s.indexOf("owl"));//获取str整段字符串中第一个字符出现的索引
			System.out.println("------------");
			
			//String substring(int start):从start开始截取字符串
			System.out.println(s.substring(2));
			System.out.println("------------");
			
			//String substring(int start,int end):从start开始,到end结束截取字符串。包括start,不包括end
			System.out.println(s.substring(0, s.length()));
			System.out.println(s.substring(0, 5));
}		
}

String的转换功能

  1. char[] toCharArry():把字符串转换成字符数组
  2. String toLowerCase():把字符转换成小写字符串
  3. String toUpperCase():把字符串转换为大写字母

eg:

public class StringDemo {
public static void main(String[] args) {
	String s = "HelloWorld";
	
	//char[] toCharArray():把字符串转换成字符数组
	char[] chs = s.toCharArray();
	for(int i =0;i<s.length();i++){
		System.out.print(chs[i]);
		if(i==s.length()-1){
			System.out.println();
		}
	}
	System.out.println("------------");
	
	//String toLowerCase():把字符串转换为大写字符串
	System.out.println(s.toUpperCase());
	
	//String toLowerCase():把字符串转换为小写字符串
	System.out.println(s.toLowerCase());
}
}

String trim():去除字符串两端的空格
String[] split(String str):按照指定符号分割字符串

eg:

public class StringDemo {
public static void main(String[] args) {
	//创建字符串对象
	String s = "helloworld";
	String s2 = "    helloworld    ";
	String s3 = "   hello   world   ";
	System.out.println("---"+s+"---");
	System.out.println("---"+s.trim()+"---");
	System.out.println("---"+s2+"---");
	System.out.println("---"+s2.trim()+"---");
	System.out.println("---"+s3+"---");
	System.out.println("---"+s3.trim()+"---");
	System.out.println("----------------");
	
	String s4 = "aa,bb,cc";
	String[] strArry = s4.split(",");
	for(int x =0;x<strArry.length;x++){
		System.out.println(strArry[x]);
	}
}
}

例题:

package com.itnineday_07;
/*
 * 把数组中的数据按照指定格式拼接成一个字符串
 * 举例:int[] arr = {1,2,3};
 * 		输出结果:[1,2,3]
 * 
 * 分析:
 * 		A:定义一个int类型的数组
 * 		B:写方法实现把数组中的元素按照指定的格式拼接成一个字符串
 * 		C:调用方法
 * 		D:输出结果
 */
public class StringTest {
public static void main(String[] args) {
	int[] arr = {1,2,3};
	
	//写方法实现把数组中的元素按照指定的格式拼接成一个字符串
	
	//调用方法
	String s = arryToString(arr);
	System.out.println("s:"+s);
}
public static String arryToString(int[] arr){
	String s = "";
	s +="[";
	for(int x =0;x<arr.length;x++){
		if(x==arr.length-1){
			s +=arr[x];
		}else{
			s+=arr[x];
			s+=",";
		}
	}
	s+="]";
	
	return s;
}
}

StringBuilder类

StringBuilder是一个可变的字符串。字符串缓冲区。
String和StringBuilder的区别
String的内容是固定的
StringBuilder的内容是可变的

构造方法:StringBuilder()

成员方法:
public int capacity(): 返回当前容量
public int length():返回长度(字符数)
容量:理论值
长度:实际值

eg:

public class StringBuilderDemo {
public static void main(String[] args) {
	StringBuilder sb = new StringBuilder();
	System.out.println("sb:"+sb);
	System.out.println("sb.capacity():"+sb.capacity());
	System.out.println("sb.length():"+sb.length());
}
}

添加功能:
public StringBuilder append(任意类型):添加数据,并返回自身对象
反转功能
public StringBuilder reverse()

eg:

public class StringBuilderDemo {
public static void main(String[] args) {
	//创建对象
	StringBuilder sb = new StringBuilder();
	/*//添加功能
	StringBuilder sb2 = sb.append("hello");
	
	System.out.println("sb:"+sb);
	System.out.println("sb2:"+sb2);
	System.out.println(sb ==sb2);//true
*/	
	sb.append("hello");
	sb.append("world");
	System.out.println("sb:"+sb);
	
	//反转功能
	sb.reverse();
	System.out.println("sb:"+sb);
}
}

引用类型和引用类型作比较 比较的是地址

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值