2020 7 28 String類的方法

2021-10-08 15:19:41 字數 4682 閱讀 9739

常見構造方法

public string():空構造

public string(byte bytes):把位元組陣列轉成字串

public string(byte bytes,int index,int length):把位元組陣列的一部分轉成字串(index:表示的是從第幾個索引開始, length表示的是長度)

public string(char value):把字元陣列轉成字串

public string(char value,int index,int count):把字元陣列的一部分轉成字串

public string(string original):把字串常量值轉成字串

b:案例演示

演示string類的常見構造方法

演示前先說乙個字串的方法:

public int length():返回此字串的長度。

package com.west.demo3;

public class stringtest ;

string str=new string(by);

system.out.println(str);//c`_/�會出現亂碼情況

//public string(byte bytes,int index,int length):

// 把位元組陣列的一部分轉成字串(index:表示的是從第幾個索引開始, length表示的是長度)

string str1=new string(by,0,4);

system.out.println(str1);//c`_/

//以下這種情況會出現索引越界

string s = new string(by,4,4);

system.out.println(s);//stringindexoutofbound***ception

}}

package com.west.demo3;

public class stringtest01 ;

string s = new string(value);

system.out.println(s);//abcd詹姆斯

for (int i = 0; i < value.length; i++)

system.out.println(s);//abcd詹姆斯[c@4554617c[c@4554617c[c@4554617c[c@4554617c[c@4554617c[c@4554617c[c@4554617c

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

//public string(char value,int index,int count):把字元陣列的一部分轉成字串

string s1 = new string(value,4,3);

system.out.println(s1);//詹姆斯*/

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

//public string(string original):把字串常量值轉成字串

int length="abc".length();

system.out.println(length);//3

string s2 = new string("abc");

int len=s2.length();

system.out.println(len);//3

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

}}

string s3 = new string("hello");

s3="world"+" dudu";

//在堆記憶體常量池開闢了兩個空間,分別有兩個位址,第二個位址將第乙個位址覆蓋。(內容不能變,引用可以變)

system.out.println(s3);//world dudu

//兩個物件指的是乙個常量池裡的同一空間

string s4 = new string("hello");

string s5="hello";

system.out.println(s4==s5);//false

package com.west.demo3;

//string類的判斷功能

public class stringtest02 else }}

}

package com.west.demo3;

//string類的轉換功能

public class stringtest04 ;

//int a=12;

//string s1=new string(string.valueof(str));

string s = new string("嘟嘟嘟");

//1,public byte getbytes():把字串轉換為位元組陣列。

/*byte by=s.getbytes();

for (int i = 0; i < s.length(); i++) */

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

char c=s.tochararray();//呼叫方法

/*char chars=new char[s.length()];//建立接收陣列的物件

for (int j = 0; j < s.length(); j++) */

string s1 = new string(c);

system.out.println(s1);

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

//拼接空串

int num=66;

string s2=num+"";

system.out.println(s2);//66

system.out.println(s.tochararray());//abc

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

string s3=string.valueof(100);

system.out.println(s3);//100

string s4 = string.valueof(false);

system.out.println(s4);//false

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

system.out.println();

//拼接字串

string s5="asd".concat("123").concat("didud");

system.out.println(s5);

}}

package com.west.demo3;

public class stringtest07

system.out.println("*************************=");

string s = new string(bytes);

system.out.println(s);

system.out.println("******************************==");*/

string str2 = "我愛你";

//把乙個字串轉換成字元陣列

// char chars = str2.tochararray();

char chars = new char[str2.length()];

for (int i = 0; i < str2.length(); i++)

string s1 = new string(chars);

system.out.println(s1);

string s2 = "abc".touppercase();

string s3 = "bcd".tolowercase();

system.out.println("***********************************====");

/* int num=100; //"100"

string str3=num+""; //拼接空串

system.out.println(str3);

boolean b=true; //"true"*/

//valueof(100); 把很多種型別轉換成字串

string s4 = string.valueof(100);

string s5 = string.valueof(false);

char chars1=;

string s6 = new string(chars1);

string s7 = string.valueof(chars);

//拼接字串。

string abc = "abc".concat("abc").concat("dddd").concat("555555");

system.out.println(abc);

}}

String 類的實現(3)String類常用函式

1 2 include3 include4 include5 include 6 using namespace std 自己模擬實現的部分相關c string庫函式 8int my strlen const char p 9 17return count 18 19char my strcopy ...

string類的實現

參考c primer.string類的實現,清翔兔 06,jan.includeusing namespace std class string string void private char m data inline string string const char str inline st...

String類的實現

學習資料結構寫了乙個string的類,貼出來求指教 ifndef string h h define string h h include include include define defaultsize 128 class string maxsize為傳入引數的string string c...