關於字串操作的方法

2021-07-02 02:45:38 字數 914 閱讀 6783

方法                               效果

s.length()                        返回s字串長度

s.charat(2)                       返回s字串中下標為2的字元

s.substring(0, 4)                 返回s字串中下標0到4的子字串

s.indexof("hello")                返回子字串"hello"的下標

s.startswith(" ")                 判斷s是否以空格開始

s.endswith("oo")                  判斷s是否以"oo"結束

s.equals("good world!")           判斷s是否等於"good world!"

==只能判斷字串是否儲存在同一位置。需要使用equals()判斷字串的內容是否相同。

s.compareto("hello nerd!")        比較s字串與"hello nerd!"在詞典中的順序,

返回乙個整數,如果<0,說明s在"hello nerd!"之前;

如果》0,說明s在"hello nerd!"之後;

如果==0,說明s與"hello nerd!"相等。

s.trim()                          去掉s前後的空格字串,並返回新的字串

s.touppercase()                   將s轉換為大寫字母,並返回新的字串

s.tolowercase()                   將s轉換為小寫,並返回新的字串

s.replace("world", "universe")    將"world"替換為"universe",並返回新的字串

操作字串方法

基礎概念 如何定義乙個字串?string 直接打或者 new乙個 使用什麼方法可以獲得字串的長度?length 如何定義乙個string buffer類的物件?new關鍵字 會使用 和equals 比較字串 位址,equals內容 會使用string buffer類方法對字串進行操作 stringb...

關於字串的相關操作

分割字串 nsstring string hello world nsarray tmpary string componentsseparatedbystring nslog tmpary 字串替換 nsstring string hello world nsstring newstring st...

關於字串操作 一

在我們開發過程中跟介面打交道時,資料用的對多的格式應該就是字串了,因為介面空間上的值通常為字串型別,變數需要轉化為字串才能給各種空間複製,從控制項中讀取資料也是字串型別。很多時候字串就是其乙個中間轉化作用。下面詳細的介紹一些字串操作。stringbuilder 類的部分屬性與方法 stringbui...