string类有可以调换方向的函数吗_String类中常用的操作
一、獲取:
1、獲取字符串的長度(注意是方法,不是跟數組的屬性一樣的)
int length();
1 public static void getLength(){
2 String s = "java01";
3 int len = s.length();
4 System.out.println(len);
5 }
2、根據位置,獲取該位置的那一個字符(只能是單個字符)
char charAt(int index);
1 //根據指定位置獲取對應的那個字符,只能是一個字符不能是字符串
2 public static void getChar(){
3 String s1 = "java01";
4 char ch = s1.charAt(3);
5 System.out.println(ch);
6 //s1.charAt(45);當訪問到字符串中不存在的角標是會發生StringIndexOutOfBoundsException,即字符串角標越界異常
7 }
3、根據字符或字符串獲取索引,即根據字符或字符串獲取該字符或字符串在整個字符串中的位置,如果字符串中沒有該字符或字符串會返回-1
int indexOf(int ch):返回的是ch在字符串中第一次出現的位置
int indexOf(int ch,int fromIndex):從fromIndex指定位置開始查找,獲取ch在字符串中出現的位置
int indexOf(String str):返回的是str在字符串中第一次出現的位置
int indexOf(String str,int fromIndex):從fromIndex指定位置開始查找,獲取str在字符串中出現的位置
1 public static void getIndexOf(){
2 String s = "javascript";
3 int x1 = s.indexOf('s');
4 int x2 = s.indexOf('a',2);
5 int x3 = s.indexOf("script");
6 int x4 = s.indexOf("script",2);
7 System.out.println("x1="+x1+",x2="+x2+",x3="+x3+",x4="+x4);
8 }
4、反向索引一個字符或字符串出現的位置
public int lastIndexOf(int ch) 從字符串的最后向前查找,指定的字符是否存在,如果存在則返回位置,如果不存在則返回-1
public int lastIndexOf(int ch,int fromIndex) 從字符串的指定的末尾向前查找,指定的字符是否存在,如果存在則返回位置,如果不存在則返回-1
1 public static void getLastIndexOf() {
2 String s = "javascript";
3 int x = s.lastIndexOf('a');
4 System.out.println("x="+x);
5}
5、獲取字符串中的一部分,即獲取子串
String substring(begin);//從指定位置開始到結尾包含頭
String substring(begin,end);//,指定開始和結尾 包含頭不包含尾
1 public static void getSubstring(){
2 String s1 = "javascript";
3 String s2 = s1.substring(4);
4 String s3 = s1.substring(0,5);
5 System.out.println("s2="+s2+",s3="+s3);
6 }
二、轉換:
1、將字符數組轉成字符串
構造函數:
String(char[])
Stirng(char[],fffset,count):將字符數組中的一部分轉換字符串
靜態方法:(直接用String類名調用,如:String s = String.copyValueOf())
static String copyValueOf(char[]);
static String copyValueOf(char[] ,int offset, int count);(offset表示起始位,count表示個數.如果超過了字符數組的長度,也發生字符串角標越界)
//ValueOf這個方法除了可以將字符數組變成字符串之外,也可以將其他基本數據類型變成字符串
static String valueOf(char[]);
static String valueOf(char[] data, int offset, int count)
1 public static void CharsToString(){
2 char[] arr ={'j','a','v','a','s','c','r','i','p','t'};
3 //通過String類的構造函數將字符數組轉成字符串(可以將字符數組的一部分轉成字符串)
4 String s1 = new String(arr);
5 String s2 = new String(arr,2,7);
6 System.out.println("s1="+s1+",s2="+s2);
7
8 //通過String類中的靜態方法將字符數組轉成字符串(可以將字符數組的一部分轉成字符串)
9 String s3 = String.copyValueOf(arr);
10 String s4 = String.copyValueOf(arr, 2, 7);
11 System.out.println("s3="+s3+", s4="+s4);
12 }
2、將字符串轉成字符數組
char[] toCharArray();
1 public static void StringToChar(){
2 String s = "javascript";
3 char[] arr = s.toCharArray();
4 for(char arrs : arr){
5 System.out.println(arrs);
6 }
7 }
3、將字節數組轉成字符串(只能用構造函數)
String(byte[])
Stirng(byte[],fffset,count):將字節數組中的一部分轉
Stirng(byte[],String charsetName)(通過使用指定的charset解碼制定的byte數組,構造一個新的String)
String info = "小白是一個無節操,無干爹,無底線的三無少年";
//把字符串轉換成字節數組
byte[] bytes = info.getBytes();
for(int i = 0;i
System.out.print(bytes[i]+",");
}
System.out.println();
//把字節數組轉換成字符串
String s1 = new String(bytes);
System.out.println("轉換后的字符串為:"+s1);
String s2 = new String(bytes,0,16);//結果是:小白是一個無節操,因為一個漢字是兩個字節,如果是15的話最后一個漢字打印的是一個? 因為找不到
System.out.println("s2="+s2);
String s3 = new String(bytes,"gbk");//系統是gbk編碼,如果這里用了utf-8會變成亂碼
System.out.println(s3);
4、將字符串轉成字節數組(字符串和字節數組在轉換過程中,是可以指定編碼表的。)
byte[] getBytes();
1 public static void StringToBytes(){
2 String s1 = "javascript";
3 byte[] arr = s1.getBytes();
4 for(byte arrs : arr){
5 System.out.println((char)arrs);
6 }
7 }
String info = "小白是一個無節操,無干爹,無底線的三無少年";
//把字符串轉換成字節數組
byte[] bytes = info.getBytes();
for(int i = 0;i
System.out.print(bytes[i]+",");
}
5、將基本的數據類型轉成字符串
static String valueOf(int)
static String valueOf(double)
3+"";???? String.valueOf(3);//這是一樣的
6、將字符串轉成大寫或者小寫
String toUpperCase();
String toLowerCase();
1 public static void ToUpper_Lower(){
2 String s1 = "javascript";
3 String s2 = s1.toUpperCase();//轉成大寫
4 String s3 = s2.toLowerCase();//轉成小寫
5 System.out.println("s2="+s2+",s3="+s3);
6 }
三、判斷:
1、字符串中是否含某一個子串(不能判斷是否包含某一個字符,只能是字符串)
boolean contains(String?str); ? ? ? ?//返回一個字符串是否存在,存在返回true 不存在返回false
特殊之處:indexOf(str)可以索引str第一次出現位置,如果返回-1,表示該str不在字符串中存在? 所以也可以用于對指定判斷是否包含 該方法既可以判斷又可以獲取出現的位置
只用于判斷時用contains,既要判斷又要獲取位置用indexOf
1 public static void containsDemo() {
2 String s = "javascript";
3 boolean b = s.contains("java");
4 System.out.println(b);
5 }
2、字符中是否有內容
boolean isEmpty(); ? ? ? //就是判斷長度是否為0
1 public static void isEmptyDemo() {
2 String s = " ";//空格也是一個字符
3 boolean b = s.isEmpty();
4 System.out.println(b);
5 }
3、字符串是否以指定的內容開頭與字符是否是以指定內容結尾(接受的是字符串)
boolean startsWith(str);
boolean startsWith(str,int toffset);//從指定的位置開始判斷是否以指定的內容開頭
boolean endsWith(str);
1 public static void startsWith_endsWith() {
2 String s = "Array.java";
3 boolean b1 = s.startsWith("Array");//開發中可以用于判斷該文件名稱是否是Array單詞開頭
4 boolean b2 =s.endsWith(".java");//開發中可以用于判斷該文件名的后綴是否是.java文件
5 System.out.println("b1="+b1+",b2="+b2);
6 }
4、判斷字符串內容是否相同
(復寫了Object類中的equals方法,定義了自己的比較方式,比較字符串內容是否相同,相同為true,不相同為false)? boolean equals(str);
1 public static void equalsDemo() {
2 String s1 = "javascript";
3 String s2 = "javascript";
4 String s3 = "html+CSS";
5 boolean b1 = s1.equals(s2);
6 boolean b2 = s1.equals(s3);
7 System.out.println("b1="+b1+",b2="+b2);
8 }
判斷內容是否相同,并忽略大小寫?? ?boolean equalsIgnoreCase();
5、對兩個字符串進行自然順序的比較(該方法能比較出誰大誰小,而equals只是比較兩個對象是否相等)
int compareTo(string);
對象之間的比較用的是compareTo方法,基本數據類型數據之間的比較用的是大于號,小于號,等于號這些運算符
1 public static void compareToDemo() {
2 String s1 = "javascript";
3 String s2 = "javascript";
4 String s3 = "java";
5 int b1 = s1.compareTo(s2);
6 int b2 = s1.compareTo(s3);
7 int b3 = s3.compareTo(s1);
8 System.out.println("b1="+b1+",b2="+b2+",b3="+b3);
9 }
四、其他常用的方法:
1、替換(將字符串中的某個字符或某部分字符串替換)
public String replace(char oldChar,char newChar) 替換指定字符
public String replace(CharSequence target,CharSequence replacement) 替換指定字符串
public String replaceAll(String regex,String replacement) 替換指定字符串
public String replaceFirst(String regex,String replacement) 替換第一個滿足條件的字符串
1 public static void replaceDemo() {
2 String s = "javascript";
3 String s1 = s.replace('a', 'b');
4 String s2 = s.replace("java", "php");
5 System.out.println("s1="+s1+",s2="+s2);
6 }
public class EightFour {
public static void main(String[] args) {
String info = "小白長a的吧,像一個背靠著d墻沒牙的,老頭在喝粥";
//替換指定字符
String s1 = info.replace(',' , ':');
System.out.println(s1);
//替換指定字符串
String s2 = info.replace("小白", "大黑菜");
System.out.println(s2);
String s3 = info.replaceAll("的", "fuck");
String s4 = info.replaceAll("[a-z]", "&");//第一個參數表示的是正則表達式,這里表示a到z的字母都可以替換成&符號
System.out.println(s3);
System.out.println(s4);
String s5 = info.replaceFirst("一", "#");//也可以像replaceAll方法一樣使用正則表達式
System.out.println(s5);
}
}
2、切割(返回的是一個字符串數組)
public String[] split(String regex) 按照指定的字符串拆分
public String[] split(String regex,int limit) 拆分字符串,并指定拆分的個數
1 public static void splitDemo() {
2 String s = "java,javascript,php";
3 String[] ss = s.split(",");//這里要是雙引號
4 for(String sss : ss){
5 System.out.println(sss);
6 }
7 }
public class EightSix {
public static void main(String[] args) {
String info = "懷才就像a懷孕-時間久了才能b-看的c出來";
String[] s1 = info.split("-");
for(int i = 0; i
System.out.println(s1[i]);
}
String[] s2 = info.split("[a-z]");//參數可以使用正則表達式
for(int i = 0; i
System.out.println(s2[i]);
}
String[] s3 = info.split("-", 0);//第二個參數表示,將這個字符串拆分為多少個字符串,如果傳入的個數超過了第一個參數的個數,那么有多少拆多少,傳入0也是有多少拆多少
for(int i = 0; i
System.out.println(s3[i]);
}
}
}
3、將字符串兩端的多個空格去除 ?,中間的空格不去
String trim();
1 public static void trimDemo(){
2 String s = " javas cript "
3 String ss = s.trim();
4 System.out.println(s);
5 System.out.println(ss);
6 }
4、字符串連接操作
public String concat(String str)
String info = " Time是把殺豬刀,黑了 木耳,紫了葡萄,軟了香蕉。 ";
String s4 = info.concat("我的fuck");
System.out.println(s4);
5、比較(比較兩個字符串內容是否相同)
equals(String str):復寫了Object中的equals方法,重新定義為比較兩個字符串的內容是否相同并區分大小寫
public class Text {
public static void main(String[] args) {
String s1 = "WeChat";
String s2 = "zhong";
System.out.println(s1.equals(s2));
}
}
6、equalsIgnoreCase(String str):
比較兩個字符串的內容是否相同,不區分大小寫
public class Text {
public static void main(String[] args) {
String s1 = "WeChat";
String s2 = "wechat";
System.out.println(s1.equalsIgnoreCase(s2));
}
}
該方法這實際應用中用的非常多,比如我們在編寫一個用戶登錄界面的時候,需要將用戶的姓名與數據庫里面的姓名進行比較,這時如果我們用equals方式來比較,就可能出錯,比如程序可能認為Tom和tom不是同一個人,這樣程序就不夠友好了,想要讓程序友好一些,不出現這樣的錯誤,就要用 方法equalsIgnoreCase來做姓名這個字段的驗證
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的string类有可以调换方向的函数吗_String类中常用的操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: airpods多少钱啊?
- 下一篇: 伸缩轨道_深度解析——伸缩喷漆房为什么这