这篇文章带你学会字符串的平常操做java
字符串在平常生活中无处不在,因此掌握字符串的使用相当重要。
使用 String 对象存储字符串,String 类位于 java.lang 包中,java.lang 不须要咱们手动导入能够直接使用。数组
敲一敲:String对象存储字符串app
String s="Hello world"; String s=new String(); String s=new String("Hello world");
下面列出一些经常使用的方法code
方法 | 介绍 |
---|---|
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 方法比较input
敲一敲:不考虑大小写比较1string
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")); } }
敲一敲:不考虑大小写比较2it
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 拼接字符
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); } }
搜索关注公众号「享智同行」,第一时间获取技术干货