zoukankan      html  css  js  c++  java
  • Java入门系列-13-String 和 StringBuffer

    这篇文章带你学会字符串的日常操作

    String类

    字符串在日常生活中无处不在,所以掌握字符串的使用至关重要。
    使用 String 对象存储字符串,String 类位于 java.lang 包中,java.lang 不需要我们手动导入可以直接使用。

    敲一敲:String对象存储字符串

    String s="Hello world";
    String s=new String();
    String s=new String("Hello world");
    

    下面列出一些常用的方法

    方法 介绍
    length() 获取字符串中字符的个数
    equals() 比较两个字符串对象的内容是否一致
    equalsIgnoreCase() 忽略大小写比较
    toLowerCase() 转小写
    toUpperCase() 转大写
    concat() 向字符串后面拼接字符串并返回一个新字符串
    indexOf() 搜索第一个出现的字符或字符串,从左往右
    lastIndexOf() 与indexOf搜索方向相反
    substring() 提取指定位置开始的字符串或指定开始和结束之间的字符串
    trim() 返回去除字符串前后的空格后的副本
    split() 按照指定字符串分隔,返回字符串数组

    敲一敲:length 方法的使用

    import java.util.Scanner;
    
    public class DemoLength {
    	public static void main(String[] args) {
    		Scanner input=new Scanner(System.in);
    		System.out.println("请输入用户名:");
    		String name=input.next();
    		if (name.length()<4) {
    			System.out.println("用户名不能小于4位");
    		}else {
    			System.out.println("用户名可以使用,长度为:"+name.length());
    		}
    	}
    }
    

    ==equals 不能混用,== 判断两个字符串在内存中的地址,在任何地方使用 new 都会产生新的内存地址。

    敲一敲:体会区别和字符串的不可变性

    public class DemoString {
    	public static void main(String[] args) {
    		String a="张三";
    		String b="张三";//a与b是同一个对象,未变
    		System.out.println(a==b);//true
    		System.out.println(a.equals(b));//true
    		
    		String c=new String("张三");//新的字符串对象
    		System.out.println(a==c);//false
    		System.out.println(a.equals(c));//true
    	}
    }
    

    比较两个字符串的值不能使用 == ,应该使用 equals 方法比较

    敲一敲:不考虑大小写比较1

    import java.util.Scanner;
    
    //忽略大小比较方法1
    public class DemoEquals1 {
    	public static void main(String[] args) {
    		Scanner input=new Scanner(System.in);
    		String str="";
    		do {
    			System.out.println("继续输入 Yes");
    			str=input.next();
    		} while (str.toLowerCase().equals("yes")||str.toUpperCase().equals("YES"));
    	}
    }
    

    敲一敲:不考虑大小写比较2

    import java.util.Scanner;
    
    public class DemoEquals2 {
    	public static void main(String[] args) {
    		Scanner input=new Scanner(System.in);
    		String str="";
    		do {
    			System.out.println("继续输入 Yes");
    			str=input.next();
    		} while (str.equalsIgnoreCase("yeS"));
    	}
    }
    

    敲一敲:对比 + 拼接和 concat() 拼接

    public class DemoConcat {
    	public static void main(String[] args) {
    		String a="hello";
    		String b=a+" world";
    		System.out.println(b);
    		String c=b.concat(" test");//b不会变
    		System.out.println(b);
    		System.out.println(c);
    	}
    }
    

    敲一敲:indexOf 和 lastIndexOf 的使用

    import java.util.Scanner;
    
    public class TestIndexOf {
    	public static void main(String[] args) {
    		System.out.println("请输入邮箱:");
    		Scanner input=new Scanner(System.in);
    		String email=input.next();
    		if(email.indexOf("@")==-1) {
    			System.out.println("邮箱格式有误!");
    		}
    		String text="a@bb@cc";
    		System.out.println("indexOf:"+text.indexOf("@"));
    		System.out.println("lastIndexOf:"+text.lastIndexOf("@"));
    	}
    }
    

    敲一敲:使用substring

    import java.util.Scanner;
    public class DemoSubstring {
    	public static void main(String[] args) {
    		//获取邮箱用户名
    		Scanner input=new Scanner(System.in);
    		System.out.println("输入完整邮箱:");
    		String email=input.next();
    		int index=email.indexOf("@");
    		String username=email.substring(0,index);
    		System.out.println("用户名:"+username);
    	}
    }
    

    敲一敲:使用 trim

    public class DemoTrim {
    	public static void main(String[] args) {
    		String text=" he llo ";
    		System.out.println(text.trim());
    	}
    }
    

    敲一敲:使用 split

    public class DemoSplit {
    	public static void main(String[] args) {
    		String text="hello,world,java";
    		String[] result=text.split(",");
    		for (int i = 0; i < result.length; i++) {
    			System.out.println(result[i]);
    		}
    	}
    }
    

    StringBuffer类与常用方法

    对字符串频繁修改(如字符串连接)时,使用 StringBuffer 类可以大大提高程序执行效率。

    敲一敲:使用 StringBuffer 拼接字符

    public class DemoStringBuffer {
    	public static void main(String[] args) {
    		StringBuffer sb=new StringBuffer("hello");
    		sb.append("word");
    		System.out.println(sb.toString());
    		
    		StringBuffer sb2=new StringBuffer();
    		sb2.append("hello");
    		sb2.append("word");
    		System.out.println(sb2);
    	}
    }
    

    敲一敲:使用 insert 方法

    import java.util.Scanner;
    
    public class TestInsert {
    	public static void main(String[] args) {
    		Scanner input=new Scanner(System.in);
    		System.out.println("请输入一串数字:");
    		String nums=input.next();
    		StringBuffer str=new StringBuffer(nums);
    		for (int i = str.length()-3; i >0; i-=3) {
    			str.insert(i, ",");
    		}
    		System.out.println(str);
    	}
    }
    

    搜索关注公众号「享智同行」,第一时间获取技术干货

  • 相关阅读:
    Python staticmethod
    pandas通过字典生成dataframe
    关于series的统计
    python的*args与**kwargs
    python global
    matplotlib画子图时设置总标题
    matplotlib两种画散点图的方式
    idea下web工程的编译和输出设置
    Vue&webpack入门实践
    《图解设计模式》读书笔记6-1 VISITOR模式
  • 原文地址:https://www.cnblogs.com/AIThink/p/9773524.html
Copyright © 2011-2022 走看看