這篇文章帶你學會字符串的平常操做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); } }
搜索關注公衆號「享智同行」,第一時間獲取技術乾貨