String類的構造方法

2021-10-19 11:41:50 字數 763 閱讀 2162

string s1 =

"hello world"

;system.out.

println

(s1)

;//hello world

byte

bytes =

;//97是a,98是b,99是c

string s2 =

newstring

(bytes)

;system.out.

println

(s2)

;//輸出結果:abc

s1變數中儲存的僅僅是乙個記憶體位址,但是string類已經重寫了tostring()方法。

因為輸出乙個引用的時候,會自動呼叫tostring方法,預設object類的話,會自動輸出物件的記憶體位址。

輸出字串物件的話,輸出的不是物件的的記憶體位址,而是字串本身。

string s3 =

newstring

(byte,1

,2)system.out.

println

(s3)

;//輸出結果:bc

string(位元組陣列,陣列元素下標的起始位置,長度)

char

chars =

;

string s = new string(chars,陣列元素下標的起始位置,長度);

String類的構造方法

字串是常量,一旦被賦值,其值不能在改變 常用的構造方法 public string 無參構造 public string byte bytes 將位元組陣列轉換成字串 public string byte bytes,int offset,int length 將位元組陣列的一部分轉換成字串 pub...

String類的構造方法

public class stringdemo2 strign char value 傳遞字元陣列 將字元陣列轉換為字串 字元陣列引數不查詢編碼表 string char valuie,int offeset,int count offeset代表陣列開始的索引 count 代表個數 public ...

String 類的構造

檔名稱 3.cpp 完成日期 2016年 5 月 24日 版本號 v1.0 專案 string類的構造 寫乙個能處理字串的類,其資料成員如下所示 include include using namespace std class string string string string string ...