温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java基础之string类的示例分析

发布时间:2021-08-10 13:41:40 来源:亿速云 阅读:129 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“java基础之string类的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“java基础之string类的示例分析”这篇文章吧。

    1、String类

    1.1两种对象实例化方式

    对于String在之前已经学习过了基本使用,就是表示字符串,那么当时使用的形式采取了直接赋值:

    public class StringText{
    	public static void main(String args[]){
    	String str =new String( "Hello");     //构造方法
    	System.out.print(str);
    	}
    }

    对于String而言肯定是一个类,那么程序之中出现的str应该就是这个类的对象,那么就证明以上的赋值操作实际上就表示要为String类的对象进行实例化操作。

    但String毕竟是一个类,那么类之中一定会存在构造方法,String类的构造:

    public class StringText{
    	public static void main(String args[]){
    	String str =new String( "Hello");     //构造方法
    	System.out.print(str);
    	}
    }

    发现现在也可以通过构造方法为String类对象实例化。

    1.2字符串比较

    如果现在有两个int型变量,如果想要知道是否相等,使用“==”进行验证。

    public class StringText{
    	public static void main(String args[]){
    	int x = 10;
    	int y = 10;
    	System.out.print(x==y);
    	}
    }

    换成String

    public class StringText{
    	public static void main(String args[]){
    		String str1 = "Hello";
    		String str2 = new String("Hello");
    		String str3 = str2;     //引用传递
    		System.out.print(str1== str2);          //false
    		System.out.print(str1== str3);          //false
    		System.out.print(str2== str3);          //ture
    	}       
    }

    java基础之string类的示例分析

    现在使用了“==”的确是完成了相等的判断,但是最终判断的是两个对象(现在的对象是字符串)判断是否相等,属于数值判断------判断的是两个对象的内存地址数值,并没有判断内容,而想要完成字符串内容的判断,则就必须使用到String类的操作方法:public Boolean equals(String str)(将方法暂时变了)

    public class StringText{
    	public static void main(String args[]){
    		String str1 = "Hello";
    		String str2 = new String("Hello");
    		String str3 = str2;     //引用传递
    		System.out.print(str1.equals(str2));          //ture
    		System.out.print(str2.equals(str3));          //ture
    		System.out.print(str2.equals(str3));          //ture
    	}       
    }

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

    如果在程序之中定义了字符串(使用“””),那么这个就表示一个String对象,因为在各个语言之中没有关于字符串数据类型的定义,而Java将其简单的处理了,所以感觉上存在了字符串数据类型。

    **范例:**验证字符串是对象的概念

    public class NiMing{
    	public static void main(String args[]){
    		String str = "Hello";
    		System.out.print("Hello".equals(str));     //通过字符串调用方法
    	}       
    }

    匿名对象可以调用类之中的方法与属性,以上的字符串可以调用了equals()方法,那么它一定是一个对象。

    **小技巧:**关于字符串与字符串常量的判断

    例如:在实际工作之中会有这样一种操作,要求用户输入一个内容,之后判断此内容是否与指定内容相同。

    public class NiMing{
    	public static void main(String args[]){
    		String str = "Hello";
            if(str.equals("Hello")){
    		    System.out.print("条件满足");
    		}   
    	}       
    }

    但,既然数据是用户自己输入,那么就有可能没有输入内容。

    public class TestDemo1{
    	public static void main(String args[]){
    		String str = null;
            if(str.equals("Hello")){
    		    System.out.print("条件满足");
    		}   
    	}       
    }
    
    //报错
        Exception in thread "main" java.lang.NullPointerException
            at NiMing.main(TestDemo1.java:4)
    //现在将代码反过来操作:
            public class TestDemo1{
    	public static void main(String args[]){
    		String str = null;
            if("Hello".equals(str)){
    		    System.out.print("条件满足");
    		}   
    	}       
    }

    因为字符串常量是匿名对象,匿名对象不可能为null。

    1.4String两种实例化方式区别

    1、分析直接赋值方式
    String str = "Hello";     //定义字符串

    java基础之string类的示例分析

    发现现在只开辟额一块堆内存空间和一块栈内存空间。

    2、构造方法赋值
    String  str = new String("Hello");

    java基础之string类的示例分析

    使用构造方法赋值的方式开辟的字符串对象,实际上会开辟两块空间,其中有一块空间就爱那个成为垃圾。

    public class TestDemo2{
    public static void main(String args[]){
    		String str1 = new String("Hello");
    		String str2 = "Hello";   //入池
    		String str3 = "Hello";  //使用池中对象
    		System.out.print(str1==str2);          //false
    		System.out.print(str2==str3);          // ture 
    		System.out.print(str1==str3);          // false
    	}       
    }

    通过上面的程序可以发现,使用构造方法实例化String对象,不会入池,只能自己使用。可是在String类之中为了方便操作提供了一种称为手工入池的方法:public String intern()。

    public class TestDemo2{
    public static void main(String args[]){
    		String str1 = new String("Hello").intern();    //手工入池
    		String str2 = "Hello";   //入池
    		String str3 = "Hello";  //使用池中对象
    	    System.out.print(str1==str2);          //ture		 	 	    			System.out.print(str2==str3);          //ture
    		System.out.print(str1==str3);          //ture
    	}       
    }

    1.5字符串常量不可改变

    字符串类的操作特点决定:字符串不可能去修改里面的内容。

    public class TestDemo3{
    	public static void main(String args[]){
    		String str = "Hello";
    		str += "World";
    		str += "!!!";
    		System.out.print(str);
    	}
    }

    java基础之string类的示例分析

    通过以上的代码可以发现,字符串内容的更改,实际上改变的是字符串对象的引用过程,那么一下的代码应该尽量避免:

    public class TestDemo3{
    	public static void main(String args[]){
    		String str = "Hello";
    		for(int x=0;x<1000;x++){
    			str += x;
    		}
    		System.out.print(str);
    	}       
    }
    • 字符串赋值只用直接赋值模式进行完成

    • 字符串的比较采用equals()方法进行实现字

    • 符串没有特殊的情况不要改变太多

    1.6开发中String必用

    任何一个类的文档由如下几个部分组成

    • 类的相关定义,包括这个类的名字,有哪些父类,有哪些接口。

    • 类的相关简介。包括基本使用

    • 成员摘要(field):属性就是一种成员,会列出所有成员的信息项

    • 构造方法说明(Constructor),列出所有构造方法的信息

    • 方法信息(Method),所有类中定义好的可以使用的方法

    • 成员、构造、方法的详细信息

    1.7字符串和字符数组

    字符串就是一个字符数组,所有在String类中有字符串转变为字符数组,字符数组转换为字符串的方法。

    方法名称类型描述
    public String(char[] value)构造将字符数组中的所有内容变为字符串
    public String(char[] value, int offset, int count)构造将字符数组中的所有内容变为字符串 offset-开始 count-个数
    public char charAt(int index)普通返回char指定字符的索引值
    public char[] toCharArray()普通将字符串转化为字符数组
    charAt方法
    public class TestDemo4{
    	public static void main(String args[]){
    		String str = "Hello";
    		System.out.println(str.charAt(0));
    		//如果现在超过了字符串的长度,则会产生异常StringIndexOutOfBoundsException
    		System.out.println(str.charAt(10));
    	}
    }

    字符串和字符数组的转化是重点

    //字符串转化为字符数组
    public class TestDemo4{
    	public static void main(String args[]){
    		String str = "helloworld";
    		char data [] = str.toCharArray();
    		for(int i = 0; i < data.length; i++){
    		data[i] -= 32;	//转大写字母简化模式更简单
    			System.out.print(data[i] + "、");
    		}
    		
    	}
    }
    //字符数组转化为字符串
    public class TestDemo4{
    	public static void main(String args[]){
    		String str = "helloworld";
    		char data [] = str.toCharArray();
    		for(int i = 0; i < data.length; i++){
    		data[i] -= 32;	//转大写字母简化模式更简单
    			System.out.print(data[i] + "、");
    		}
    		System.out.println();
    		System.out.println(new String(data));//字符串数组全部转化为字符数组
    		System.out.println(new String(data,1,4));//字符串数组部分转化为字符数组
    	}
    }

    java基础之string类的示例分析

    判断字符串是否由数字组成

    public class TestDemo5{
    	public static void main(String args[]){
    		String str1 = "helloworld";
    		String str = "1234567890";
    		Judgenum(str);
    		Judgenum(str1);
    	}
    	public static void Judgenum(String str){
    		char data [] = str.toCharArray();
    		boolean judge = true;
    		for(int i = 0; i < data.length; i++){
    			if(data[i]>= '0' && data[i]<= '9'){
    				judge = false;
    			}
    		}
    		if(judge){
    			System.out.println(str+"是由字母组成");
    		}else
    			System.out.println(str+"是由数字组成");
    	}
    }

    java基础之string类的示例分析

    1.8字节和字符串

    方法名称类型描述
    public String(byte[] bytes)构造将部分字节数组变为字符串
    public String(byte[] bytes, int offset,int length)构造将部分字节数组变为字符串 bytes——要解码为字符的字节 offset——要解码的第一个字节的索引 length——要解码的字节数
    public byte[] getBytes()普通将字符串变为字节数组
    public byte[] getBytes(String charsetName) throws UnsupportedEncodingException普通编码转换编码
    //将字符串通过字节流转化为大写
    public class TestDemo6{
    	public static void main(String args[]){
    		String str = "helloworld";
    		byte data [] = str.getBytes();//字符串转换为字节数组
    		for(int i = 0; i < data.length ; i++){
    			System.out.print(data[i]+"、");
    			data[i] -= 32;
    		}
    		System.out.println(new String(data));//字节数组转化为字符串
    	}
    }

    java基础之string类的示例分析

    一般情况下,在程序之中如果想要操作字节数组只有两种情况:

    **1、**需要进行编码的转化;

    2、 数据要进行传输的时候。

    **3、**二进制文件适合字节处理

    1.9字符串比较

    方法名称类型描述
    public boolean equals(String anObject)普通区分大小写比较
    public boolean equalsIgnoreCase(String anotherString)普通不区分大小写比较
    public int compareTo(String anotherString)普通比较两个字符串的大小关系

    如果现在要比较两个字符串的大小关系,那么就必须使用comepareTo()方法完成,而这个方法返回int型数据,而这个int型数据有三种结果:大于(返回结果大于0)、小于(返回结果小于0)、等于(返回结果为0).

    public class CompareTo{
    	public static void main(String args[]){
    	String str1 = "HELLO";
    	String str2= "hello";
    	System.out.println(str1.compareTo(str2));
    	
    	}
    }

    1.10字符串查找

    方法名称类型描述
    public boolean contains(String s)普通判断一个子字符串是否村存在
    public int indexOf(String str)普通返回字符串中第一次出现字符串的索引
    public int indexOf(String str, int fromIndex)普通从指定地方开始查找子字符串的位置
    public int lastIndexOf(String str)普通从后向前查找子字符串的位置
    public int lastIndexOf(String str, int fromIndex)普通从指定位置由后向前查找
    public boolean startsWith(String prefix)普通从头判断是否以某字符串开头
    public boolean startsWith(String prefix,int toffset)普通从指定位置判断是否以字符串开头
    public boolean endsWith(String suffix)普通判断以某字符串结尾
    public class TestDemo7{
    	public static void main(String args[]){
    		String str = "helloworld";
    		System.out.println(str.contains("world"));		//true
    		//使用indexOf()进行查找
    		System.out.println(str.indexOf("world"));
    		System.out.println(str.indexOf("java"));
    		//JDK1,5之前这样使用
    		if(str.indexOf() != -1){
    			System.out.println("可以查找到指定的内容");
    		}
    	}
    }
    • 基本上所有的查找现在都是通过contains()方法完成。

    • 需要注意的是,如果内容重复indexOf()它只能返回查找的第一个位置。

    • 在进行查找的时候往往会判断开头或结尾。

    public class TestDemo7{
    	public static void main(String args[]){
    		String str = "**@@helloworld##";
    		System.out.println(str.startsWith("**"));	//true
    		System.out.println(str.startsWith("@@",2));	//true
    		System.out.println(str.endsWith("##"));	//true
    	}
    }

    1.11字符串的替换

    方法名称类型描述
    public String replaceAll(String regex,String replacement)普通替换所有的内容
    public String replaceFirst(String regex,String replacement)普通替换首内容
    public class TestDemo7{
    	public static void main(String args[]){
    		String str = "**@@helloworld##";		
    		System.out.println(str.replaceAll("l","_"));	//**@@he__owor_d##
    	}
    }

    1.12字符串的拆分

    方法名称类型描述
    public String[] split(String regex)普通将字符串全部拆分
    public String[] split(String regex,int limit)普通将字符串部分拆分






    public class TestDemo8{
    	public static void main(String args[]){
    		String str = "hello world hello zsr hello csdn";
    		String result [] = str.split(" ");	//按照空格进行拆分
    		//hello、world、hello、zsr、hello、csdn、
    		for(int i = 0; i < result.length ; i++){
    			System.out.print(result[i]+"、");	
    		}
    		System.out.println();
    		//部分拆分
    		String result1 [] = str.split(" ",3);	//按照空格进行拆分
    		//第二个参数 从第几个位置开始不进行拆分操作
    		//hello、world、hello zsr hello csdn、
    		for(int i = 0; i < result1.length ; i++){
    			System.out.print(result1[i]+"、");	
    		}
    	}
    }
    //拆分ip地址
    public class TestDemo9{
    //吴国发现内容无法拆分,就需要用到“\\”进行转义
    	public static void main(String args[]){
    	//120
    	//11
    	//219
    	//223:57114
    		String str = "120.11.219.223:57114";
    		String result [] = str.split("\\.");
    		for(int i = 0 ; i < result.length ; i++){
    			System.out.println(result[i]);
    		}
    	}
    }

    1.13字符串的截取

    方法名称类型描述
    public String substring(int beginIndex)普通从指定位置截取到结尾
    public String substring(int beginIndex,int endIndex)普通截取部分内容
    public class TestDemo10{
    	public static void main(String args[]){
    		String str = "helloworld";
    		//world
    		System.out.println(str.substring(5));
    		//hello
    		System.out.println(str.substring(0,5));	
    	}
    }

    1.14其他操作方法

    方法名称类型描述
    Public String trim()普通去掉左右空格,保留中间空格
    public String toUpperCase()普通将全部字符串转大写
    public String toLowerCase()普通将全部字符串转小写
    public String intern()普通字符串入对象池
    public String concat()普通字符串连接

    思考题:

    1.现在给出了如下一个字符串格式:“姓名:成绩|姓名:成绩|姓名:成绩”,例如:给定的字符串是:“Tom:90|Jerry:80|tony:89”,要求可以对以上数据进行处理,将数据按照如下的形式显示:姓名:Tom,成绩:90;

    public class Exam1{
    	public static void main(String args[]){
    		String str = "Tom:90|Jerry:80|tony:89";
    		String data [] = str.split("\\|");
    		for(int i = 0 ; i < data.length ; i++){
    			String result [] = data[i].split(":");
    			System.out.print("姓名 = " + result[0] + ",");
    			System.out.println("年龄 = " + result[1]);
    		}
    		
    		/*姓名 = Tom,年龄 = 90
    		  姓名 = Jerry,年龄 = 80
    		  姓名 = tony,年龄 = 89*/
    	}
    }

    2.给定一个email地址,要求验证其是否正确,提示:可以简单的验证一下,重点验证“@”和“.”。标准如下:

    1.email长度不短于5

    2.@和.不能做开头或结尾

    3.@和.顺序要有定义

    public class Exam2{
    	public static void main(String args[]){
    		String email = "1016942589.@qqcom";
    		char date[] = email.toCharArray();
    		if (date.length>5&&email.startsWith("@")==false 
    		&& email.startsWith(".")==false && email.endsWith("@")==false
    		&&email.endsWith(".")==false && email.indexOf(".") >email.indexOf("@"))
    		{System.out.println("正确");
    		}else{System.out.println("错误");}
    	}
    }

    以上是“java基础之string类的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI