java面向对象使用字符串_java面向对象中的String类中12种常用的方法
1、字符串與字符數組的轉換
字符串可以使用toCharArray()方法變成一個字符數組,也可以使用String類的構造方法把一個字符數組變成一個字符串。
public class StringAPIDemo01
{
public static void main(String[] args)
{
String str1 = "hello"; //定義字符串
char c[] = str1.toCharArray(); //將字符串變為字符數組
for(int i=0;i
System.out.println(c[i] + "\t");
}
System.out.println("");
String str2 = new String(c); //將全部字符數組變為String
String str3 = new String(c,0,3); //將部分字符數組變為String
System.out.println(str2);
System.out.println(str23);
}
}
程序運行結果:
h e l l o
hello
hel
2、從字符串中取出指定位置的字符
直接使用String類中的charAt()方法取出字符串指定位置的字符,例如。
public class StringAPIDemo02
{
public static void main(String[] args)
{
String str1 = "hello";
System.out.println(str1.charAt(3)); //取出字符串中第4個字符
}
}
程序運行結果:
l
3、把一個字符串變成一個byte數組,也可以把一個byte數組變成一個字符串。
字符串可以通過getBytes()方法將String變為一個byte數組,然后可以通過String的構造方法將一個字節數組重新變為字符串,例如:
public class StringAPIDemo03
{
public static void main(String[] args)
{
String str1 = "hello";
byte b[] = str1.getBytes(); //將字符串變為byte數組
System.out.println(new String(b)); //將全部byte數組變為字符串
System.out.println(new String(b,1,3)); //將部分byte數組變為字符串
}
}
程序運行結果:
hello
ell
4、取得一個字符串的長度
在String中使用length()方法取得字符串的長度,例如:
public class StringAPIDemo04
{
public static void main(String[] args)
{
String str1 = "hello chenjunlin";
byte b[] = str1.getBytes(); //定義字符串變量
System.out.println("\"" + str1 + "\t" 的長度為:" + str1.length());
}
}
程序運行結果:
"hello chenjunlin" 的長度為:15
注:length與length()區別,在數組操作中,使用length取得數組的長度,但是操作的最后沒有(),而字符串調用length是一個方法,只要是方法后面都有“()”。
5、查找一個指定的字符串是否存在
在String中使用indexOf()方法,可以返回指定的字符串位置,如果不存在則返回-1,例如:
public class StringAPIDemo05
{
public static void main(String[] args)
{
String str1 = "chenjunlin";
System.out.println(str1.indexOf("c")); //查找返回位置
System.out.println(str1.indexOf("c",3)); //查到返回位置,從第4個開始查找
System.out.println(str1.indexOf("x")); //沒有查到返回-1
}
}
6、去掉左右空格
在開發過程中,用戶輸入的數據中可能含有大量的空格,使用trim()方法可以去掉字符串左右空格,例如:
public class StringAPIDemo06
{
public static void main(String[] args)
{
String str1 = " chenjunlin ";
System.out.println(str1.trim()); //查找返回位置
//System.out.println(str1.indexOf("c",3)); //查到返回位置,從第4個開始查找
//System.out.println(str1.indexOf("x")); //沒有查到返回-1
}
}
7、字符串截取
在String中提供了兩個substring()方法,一個是從指定位置截取到字符串結尾,另一個是截取指定范圍內的內容,例如:
public class StringAPIDemo07
{
public static void main(String[] args)
{
String str1 = "hello world";
System.out.println(str1.substring(6)); //從第7個位置開始截取
System.out.println(str1.substring(0,5)); //截取0~5個位置的內容
}
}
程序運行結果:
world
hello
8、按照指定的字符串拆分字符串
在String中通過split()方法可以進行字符串的拆分操作,拆分的數據將以字符串數組的形式返回,例如:
public class StringAPIDemo08
{
public static void main(String[] args)
{
String str1 = "hello world"; //將空格進行字符串的拆分
String s[] = str1.split(" ");
for(int i=0;i
System.out.println(s[i]);
}
}
}
程序運行結果:
hello
world
9、字符串的大小寫轉換
在用戶輸入信息是,有時需要統一輸入數據的大小寫,此時使用toUpperCase()和toLowerCase()兩個方法完成大小寫的轉換操作,例如:
10、判斷是否以指定的字符串開頭和結尾
在String中使用startsWith()方法可以判斷字符串是否以指定的內容開頭,使用endsWith()方法可以判斷字符串是否以指定的內容結尾,例如:
11、不區分大小寫進行字符串比較
在String中可以通過equals()方法進行字符串內容的比較,但這種比較方法是區分大小寫的比較,如果要完成不區分大小寫的比較可以使用equalsIgnoreCase()方法,例如:
12、將一個指定的字符串替換成其他的字符串
使用string的replaceAll()方法可以將字符串的指定內容進行替換,例如:
總結
以上是生活随笔為你收集整理的java面向对象使用字符串_java面向对象中的String类中12种常用的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 用户概念_传输概念 – d
- 下一篇: 魅族 Flyme 中文 OS 名大投票活