Java中的字符串操作类【String】

Java中的字符串操作类【String】

7.1.String类如何创建对象,有哪些常用方法?

     String类---表示一个字符串类【处理字符串数据的操作方法】

     String类是使用final修饰符修饰的,说明它没有子类,不可被继承。

     String类的构造方法:

  1. String() 初始化新创建的 String对象,使其表示空字符序列。
  2. String(byte[] bytes, int offset, int length) 通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。
  3. String(char[] value, int offset, int count) 分配一个新的 String ,其中包含字符数组参数的子阵列中的字符。 
  4. String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本

       例如:

package com.wangxing.stringdemo1;

public class StringTest1 {
	public static void main(String args[]){
		/*
		 * 1.String类的构造方法
		 * 		1.String() 初始化新创建的 String对象,使其表示空字符序列。 
				2.String(byte[] bytes, int offset, int length) 通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。 
				3.String(char[] value, int offset, int count) 分配一个新的 String ,其中包含字符数组参数的子阵列中的字符。 
				4.String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本。
				String(StringBuffer buffer) 分配一个新的字符串,其中包含当前包含在字符串缓冲区参数中的字符序列。 
				String(StringBuilder builder) 分配一个新的字符串,其中包含当前包含在字符串构建器参数中的字符序列。 
		 */
		//1.String() 初始化新创建的 String对象,使其表示空字符序列。 
			String str1=new String();
			str1="hello";
			System.out.println(str1);
		//2.String(byte[] bytes, int offset, int length)
			//byte[] bytes--字节数组
			//int offset--字节数组的开始位置
			//int length--字节数组中的元素个数
			//将从offset开始的length个字节数组中的数据转换成String对象
			//将字节数组中数字对应的字符数据得到并转换成String
			byte byte1[]={97,98,99,100};
			String str2=new String(byte1,0,byte1.length);
			System.out.println("str2=="+str2);
			String str21=new String(byte1,0,3);
			System.out.println("str21=="+str21);
			String str22=new String(byte1,1,3);
			System.out.println("str22=="+str22);
		//3.String(char[] value, int offset, int count)
			//char[] value--字符数组
			//int offset-----字符数组的起始位置
			//int count-----字符数组中的元素个数
			//将从offset开始的count个字符数组中的数据转换成String对象
			char char1[]={'w','o','r','l','d'};
			String str3=new String(char1,0,char1.length);
			System.out.println("str3=="+str3);//world
			String str4=new String(char1,1,3);
			System.out.println("str4=="+str4);//orl
		//4.String(String original) 利用字符串常量值创建String对象
		//String origina---字符串常量,被双引号包围的数据就是字符串常量
			//1.例如:"zhangsan"    "1234"   "true"
			String str5=new String("hello");
			//2.通过直接赋值字符串常量的方式创建String对象
			String str6="world";
			
	}
	
}

     String的常用方法:

      1.char charAt(int index) 从原始字符串中得到指定位置的字符元素。

      2.String concat(String str)将指定的字符串连接到该字符串的末尾。

      3.boolean contains(CharSequence s)判断指定的字符串数据是否在原始字符串中存在

      4.boolean endsWith(String suffix)测试此字符串是否以指定的后缀结尾。

      5.boolean startsWith(String prefix)测试此字符串是否以指定的前缀开头。

      6.byte[] getBytes() 将字符串通过默认的字符编码转换成字节数组

        byte[] getBytes(String charsetName)将字符串通过指定的字符编码转换成字节数组

      7.int indexOf(String str) 返回指定子字符串第一次出现在字符串内的索引位置

      8.lastIndexOf(String str)返回指定子字符串最后一次出现的字符串中的索引。

      9.boolean isEmpty()判断字符串是否为空串,此方法为true时,字符串长度一定为0

     10.int length() 返回此字符串的长度。

     11.boolean matches(String regex) 判断字符串数据是否符合正则表达式

     12.String replace(CharSequence old, CharSequence new) 将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列

     13.String[] split(String regex)通过指定的符号将字符串拆分成一个字符串数组

     14.String substring(int beginIndex)截取从指定的开始位置到字符串结尾的一个子字符串

        String substring(int beginIndex, int endIndex) 截取从指定的开始位置到指定的结束位置的一个子字符串

     15.char[]  toCharArray() 将此字符串转换为新的字符数组

     16.String toLowerCase() 大写转小写

     17.toUpperCase() 小写转大写

     18.String trim()去除原始字符串的两头空格

      例如:

package com.wangxing.stringdemo1;

import javax.swing.plaf.synth.SynthStyle;

public class StringTest2 {

	public static void main(String[] args) {
		String str="helloworld";
		//String的常用方法:
		//1.char	charAt(int index) 从原始字符串中得到指定位置的字符元素。
		System.out.println("charAt(int index)=="+str.charAt(0));//h
		System.out.println("charAt(int index)=="+str.charAt(4));//o
		//2.String concat(String str)将指定的字符串连接到该字符串的末尾。
			//例1:
			String str1="1234";
			System.out.println("String concat=="+str.concat(str1));
			//例2:
			String concat1=str.concat("5678");
			System.out.println("concat1=="+concat1);					
		//3.boolean contains(CharSequence s)判断指定的字符串数据是否在原始字符串中存在
			System.out.println("contains=="+str.contains("abc"));//false
			System.out.println("contains=="+str.contains("llo"));//true		
		//4.boolean endsWith(String suffix)测试此字符串是否以指定的后缀结尾。
			System.out.println("endsWith=="+str.endsWith("abc"));//false
			System.out.println("endsWith=="+str.endsWith("rld"));//true
		//5.boolean startsWith(String prefix)测试此字符串是否以指定的前缀开头。
			System.out.println("startsWith=="+str.startsWith("abc"));//false
			System.out.println("startsWith=="+str.startsWith("hell"));//true
		//6.byte[] getBytes() 将字符串通过默认的字符编码转换成字节数组
		//byte[] getBytes(String charsetName)将字符串通过指定的字符编码转换成字节数组
			byte byte6[]=str.getBytes();
			for(byte byte61:byte6){
				System.out.println(byte6+",");
			}		
		//7.int indexOf(String str) 返回指定子字符串第一次出现在字符串内的索引位置
			System.out.println("indexof=="+str.indexOf("w"));//5
			System.out.println("indexof=="+str.indexOf("l"));//2		
		//8.lastIndexOf(String str)返回指定子字符串最后一次出现的字符串中的索引。
			System.out.println("lastIndexof=="+str.lastIndexOf("w"));//5
			System.out.println("lastIndexof=="+str.lastIndexOf("l"));//8				
		//9.boolean isEmpty()判断字符串是否为空串,此方法为true时,字符串长度一定为0
			System.out.println("isEmpty=="+str.isEmpty());//false	
		//10.int length() 返回此字符串的长度。
			System.out.println("str.length=="+str.length());//10		
		//11.boolean matches(String regex) 判断字符串数据是否符合正则表达式
								
		//12.String replace(CharSequence old, CharSequence new) 将与字面目标序列匹配的字符 
        //串的每个子字符串替换为指定的字面替换序列
			System.out.println("replace=="+str.replace("hello","main"));//mainworld		
		//13.String[] split(String regex)通过指定的符号将字符串拆分成一个字符串数组
			String info="name=lisi,age=25,adress=beijing";
			String info1[]=info.split(",");
			for(String info2:info1){
				System.out.println(info2);
			}		
		//14.String substring(int beginIndex)截取从指定的开始位置到字符串结尾的一个子字符串
		//String substring(int beginIndex, int endIndex) 截取从指定的开始位置到指定的结束位 
        //置的一个子字符串
			String str2=str.replace("main", "hello");
			System.out.println("substring=="+str2.substring(5));//world
			System.out.println("substring=="+str2.substring(0,5));							
		//15.char[]  toCharArray() 将此字符串转换为新的字符数组
			char cha[]=str2.toCharArray();
			for(char cha1:cha){
				System.out.println(cha1+",");
			}		
		//17.toUpperCase() 小写转大写
		//16.String toLowerCase() 大写转小写
			System.out.println("toUpperCase=="+str2.toUpperCase());
			System.out.println("toLowerCase=="+str2.toLowerCase());
		//18.String trim()去除原始字符串的两头空格
			String nameinfo="    zhang   san    ";
			String newstr=nameinfo.trim();
			System.out.println(newstr);

	}

}

7.2.什么是封装类?

     1.只有8种基本数据类型才有与之对应的封装类类型

     2.8种基本数据类型对应的复合数据类型【对象型】

基本数据类型

封装类类型

byte[字节型]

Byte[类]

short[短整型]

Short[类]

int[整型]

Integer[类]

long[长整型]

Long[类]

float[单精度浮点型]

Float[类]

double[双精度浮点型]

Double[类]

char[字符型]

Character[类]

boolean[布尔型]

Boolean[类]

  1. 基本数据类型没有可供调用的方法和变量。封装类有可供调用的变量和方法

         例如:

package com.wangxing.fengzhuanglei;

public class FengZhuangTest {
	    static Integer integer1;
	    static int int1;
    public static void main(String[] args) {
		//1.基本数据类型,没有可供调用的方法和变量。封装类有可供调用的方法和变量
			Integer integer=new Integer(50);
			System.out.println(integer.MAX_VALUE);
			int num=50;
			//System.out.println(num.MAX_VALUE);//错误表达方式
     }
}

     2.基本数据类型只在内存的栈区有数据分配,封装类在【复合数据类型】内存的栈区和堆区都有内存分配。

     3.默认初始值不同 int---0  Integer---null

package com.wangxing.fengzhuanglei;

public class FengZhuangTest {
	static Integer integer1;
	static int int1;
	public static void main(String[] args) {
        //3.默认初始值不同int---0  Integer---null	
			System.out.println(integer1);//null
			System.out.println(int1);//0
      }
}

      基本数据类型与对应封装类的区别

基本数据类型

封装类

数据类型

变量,没可供调用的方法和变量

构造出来都是对象,提供了变量和方法

只在内存的栈区有数据分配

内存的栈区和堆区都有内存分配。

有各自默认的数据值

默认值为null

     4.封装类使用的时候要创建对象,new+构造方法

7.3.什么是自动装箱和自动拆箱?

package com.wangxing.fengzhuanglei;

public class FengZhuangTest {
	static Integer integer1;
	static int int1;
	public static void main(String[] args) {
 //自动装箱--将基本数据类型转换成对应的封装数据类型
			boolean  booln1=false;
			//1.封装类的构造方法
			Boolean boo1=new Boolean(booln1);
			//此时boo1是一个对象,可以调用方法
			//2.将基本数据类型变量/数据值直接赋值给对应的封装类变量
			Boolean boo2=booln1;//自动装箱
			Boolean boo3=true;//自动装箱
 //自动拆箱--将封装类类型转成对应的基本数据类型
			Character  c=new Character('A');
			char char1=c;

      }
}

7.4.String类与基本数据类型之间的相互转换

package com.wangxing.stringdemo2;

public class StringTest3 {
	public static void main(String args[]){
		//String与其他类型的转换
		//1.将基本数据类型转换成String
		//String【static String valueOf(基本数据类型的数据值/变量)】
		int num=100;
		String str1=String.valueOf(num);
		System.out.println(str1);
		//2.将String转换成基本数据类型[需要依赖于基本数据类型对应的封装类]
		String str2="1234";//字符串
		Integer int1=new Integer(str2);//封装类
		int int2=int1;//自动拆箱得到基本数据类型
		System.out.println(int2);//1234
		//2.2 封装类的parseXXXXX(字符串);
		String doublestring="125.8";//字符串
		double  doulble1=Double.parseDouble(doublestring); //基本数据类型
		System.out.println(doulble1);					
	}
}

7.5.String类与字节数组或者字符数组之间的相互转换

       String类与字节数组:

package com.wangxing.stringdemo2;

public class StringTest3 {
	public static void main(String args[]){
        //String与字节数组的转换
		//1.将字节数组转换成String
		//String类的构造方法
		//String(byte[] bytes, int offset, int length)
		byte byte1[]={11,22,33,44,55};
		String string1=new String(byte1,0,byte1.length);
		System.out.println(string1);
		//2.将String转换成字节数组
		String str3="12345";
		byte by2[]=str3.getBytes();
		for(byte by3:by2){
			System.out.println(by3);
		}
    }
}

      String类与字符数组:

package com.wangxing.stringdemo2;

public class StringTest3 {
	public static void main(String args[]){
        //String与字符数组的转换
		//1.将字符数组转换成String
		//String(char[] value, int offset, int count) 
		char cha1[]={'a','b','c','d'};
		String str4=new String(cha1,0,cha1.length);
		System.out.println(str4);
		//2.将String转换成字符数组
		//char[]  toCharArray() 将此字符串转换为新的字符数组
		String str5="hello";
		char char5[]=str5.toCharArray();
		for(char char6:char5){
			System.out.print(char6+"'");
		}
		
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值