String的轉換功能

2021-07-15 21:56:12 字數 1598 閱讀 9191

package cn.itcast_05;

/** string的轉換功能:

* byte getbytes():把字串轉換為位元組陣列。

* char tochararray():把字串轉換為字元陣列。

* static string valueof(char chs):把字元陣列轉成字串。

* static string valueof(int i):把int型別的資料轉成字串。

* 注意:string類的valueof方法可以把任意型別的資料轉成字串。

* string tolowercase():把字串轉成小寫。

* string touppercase():把字串轉成大寫。

* string concat(string str):把字串拼接。

*/public class stringdemo

system.out.println("----------------");

// char tochararray():把字串轉換為字元陣列。

char chs = s.tochararray();

for (int x = 0; x < chs.length; x++)

system.out.println("----------------");

// static string valueof(char chs):把字元陣列轉成字串。

string ss = string.valueof(chs);

system.out.println(ss);

system.out.println("----------------");

// static string valueof(int i):把int型別的資料轉成字串。

int i = 100;

string sss = string.valueof(i);

system.out.println(sss);

system.out.println("----------------");

// string tolowercase():把字串轉成小寫。

system.out.println("tolowercase:" + s.tolowercase());

system.out.println("s:" + s);

// system.out.println("----------------");

// string touppercase():把字串轉成大寫。

system.out.println("touppercase:" + s.touppercase());

system.out.println("----------------");

// string concat(string str):把字串拼接。

string s1 = "hello";

string s2 = "world";

string s3 = s1 + s2;

string s4 = s1.concat(s2);

system.out.println("s3:"+s3);

system.out.println("s4:"+s4);}}

String類的轉換功能

定義乙個字串 string s1 abc byte getbytes 將字串轉換成位元組陣列 byte bys s1.getbytes 97,98,99 for int i 0 i字串 123 此為靜態方法,可以直接呼叫 string s2 string.valueof 123 system.out...

String類獲取功能,轉換功能和一些其他功能

一 獲取功能 int length 獲取字串長度 public char charat int index 獲取指定索引出的字元.public int indexof int ch 返回指定字元在此字串中第一次出現處的索引.public int indexof string str 子字串在大字串中...

String常見功能

1.1 獲取字串的長度 int length 獲取長度 返回型別為int 1.2 根據位置獲取位置上某個字元 char charat int index 如果下標超出字串長度,則爆字串下標越界異常 1.3 根據字元獲取該字元在字串中位置 int indexof int ch 返回的是ch在字串中第一...