java number轉string_Java Number類, Character類,String類

字符串在Java編程中廣泛使用,字符串就是一系列字符(由一個個的字符組成)。 在Java編程語言中,字符串被視為對象。

Java平臺提供String類來創建和操作字符串。

1. 創建字符串

創建字符串的最直接方法是 -

String str = "Hello world!";

每當它在代碼中遇到字符串文字時,編譯器就會創建一個String對象,在本例中str對象的值為Hello world!。

與其他對象一樣,可以使用new關鍵字和構造函數來創建String對象。String類有11個構造函數,方便使用不同的源(例如:字符數組)提供字符串的初始值。

示例

public classStringDemo {public static voidmain(String args[]) {char[] helloArray = { 'Y', 'i', 'i', 'b', 'a', 'i'};

String helloString= newString(helloArray);

System.out.println( helloString );

}

}

執行上面示例代碼,得到下結果:

Yiibai

注 - String類是不可變的,因此一旦創建,就無法更改String對象。 如果想要對字符串進行大量修改,則應使用StringBuffer和StringBuilder。

2. 字符串長度

用于獲取對象信息的方法稱為訪問器方法。 可以與字符串一起使用來獲取字符串長度的一個訪問器方法是length()方法,它返回字符串對象中包含的字符數。

以下程序是String類的length()方法的示例。

public classStringDemo {public static voidmain(String args[]) {

String greeting= "Hi,Welcome to Yiibai.com";int len =greeting.length();

System.out.println( greeting+" 字符串的長度是: " +len );

}

}

執行上面示例代碼,得到下結果:

Hi,Welcome to Yiibai.com 字符串的長度是: 24

3. 連接字符串

String類包含一個用于連接兩個字符串的方法 -

string1.concat(string2);

這將返回一個新字符串:string1,并且string1在結尾處添加了string2。 還可以將concat()方法與字符串文字一起使用,例如 -

"My name is ".concat("Maxsu");

字符串通常使用+運算符連接,如 -

"Hello," + " world" + "!"

上面代碼執行后得到的結果是:

"Hello, world!"

下面再來看另一個例子 -

public classStringDemo {public static voidmain(String args[]) {

String string1= "Bai";

System.out.println("Yii" + string1 + ".com");

}

}

上面代碼執行后得到的結果是:

YiiBai.com

3. 創建格式化字符串

Java中使用printf()和format()方法來打印帶有格式化數字的輸出。 String類有一個等效的類方法format(),它返回一個String對象而不是一個PrintStream對象。

使用String的static format()方法可以創建重用的格式化字符串,而不是一次性打印語句。 例如 -

System.out.printf("The value of the float variable is " +

"%f, while the value of the integer " +

"variable is %d, and the string " +

"is %s", floatVar, intVar, stringVar);

上面打印語句可使用格式化寫為:

String fs;

fs= String.format("The value of the float variable is " +

"%f, while the value of the integer " +

"variable is %d, and the string " +

"is %s", floatVar, intVar, stringVar);

System.out.println(fs);

4. String類方法

以下是String類定義的方法列表 -

編號方法描述

1

char charAt(int index)

返回指定索引處的字符。

2

int compareTo(Object o)

將此String對象與另一個對象進行比較。

3

int compareTo(String anotherString)

按字典順序比較兩個字符串。

4

int compareToIgnoreCase(String str)

按字典順序比較兩個字符串,但不區分大小寫。

5

String concat(String str)

將指定的字符串連接到此字符串的末尾。

6

boolean contentEquals(StringBuffer sb)

當且僅當此String表示的字符串與指定的StringBuffer相同的字符序列時,才返回true。

7

static String copyValueOf(char[] data)

返回表示指定數組中字符序列的String對象形式。

8

static String copyValueOf(char[] data, int offset, int count)

返回表示指定數組中字符序列的String對象形式。

9

boolean endsWith(String suffix)

判斷此字符串是否以指定的字符作為后綴結尾。

10

boolean equals(Object anObject)

將此字符串與指定的對象進行比較。

11

boolean equalsIgnoreCase(String anotherString)

將此String與另一個String進行比較,忽略大小寫。

12

byte getBytes()

使用平臺的默認字符集將此String編碼為字節序列,將結果存儲到新的字節數組中。

13

byte[] getBytes(String charsetName)

使用指定的字符集將此String編碼為字節序列,將結果存儲到新的字節數組中。

14

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

將此字符串中的字符復制到目標字符數組中。

15

int hashCode()

返回此字符串的哈希碼。

16

int indexOf(int ch)

返回指定字符在此字符串中第一次出現的索引。

17

int indexOf(int ch, int fromIndex)

返回指定字符在此字符串中第一次出現的索引,它從指定索引處開始搜索。

18

int indexOf(String str)

返回指定子字符串在此字符串中第一次出現的索引。

19

int indexOf(String str, int fromIndex)

從指定的索引處開始,返回指定子字符串在此字符串中第一次出現的索引。

20

String intern()

返回字符串對象的規范表示。

21

int lastIndexOf(int ch)

返回指定字符在此字符串中最后一次出現的索引。

22

int lastIndexOf(int ch, int fromIndex)

返回指定字符在此字符串中最后一次出現的索引,它從指定的索引開始向后搜索。

23

int lastIndexOf(String str)

返回指定子字符串在些字符串中最后出現的索引。

24

int lastIndexOf(String str, int fromIndex)

返回指定子字符串在此字符串中最后一次出現的索引,它從指定索引開始向后搜索。

25

int length()

返回此字符串的長度。

26

boolean matches(String regex)

判斷此字符串是否與給定的正則表達式匹配。

27

boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

判斷兩個字符串區域是否相等。

28

boolean regionMatches(int toffset, String other, int ooffset, int len)

判斷兩個字符串區域是否相等。

29

String replace(char oldChar, char newChar)

返回一個新字符串,該字符串是使用newChar替換此字符串中出現的所有oldChar后的字符串。

30

String replaceAll(String regex, String replacement)

將替換此字符串中匹配給定正則表達式的每個子字符串。

31

String replaceFirst(String regex, String replacement)

將替換此字符串中第一個匹配給定正則表達式的子字符串。

32

String[] split(String regex)

將此字符串拆分為給定正則表達式的匹配項。

33

String[] split(String regex, int limit)

將此字符串拆分為給定正則表達式的匹配項。

34

boolean startsWith(String prefix)

判斷此字符串是否以指定的字符串前綴開頭。

35

boolean startsWith(String prefix, int toffset)

判斷此字符串在指定的索引是否以指定的前綴開始。

36

CharSequence subSequence(int beginIndex, int endIndex)

返回一個新的字符序列,它是該序列的子序列。

37

String substring(int beginIndex)

返回一個新字符串,該字符串是此字符串的子字符串。

38

String substring(int beginIndex, int endIndex)

返回一個新字符串,該字符串是此字符串的子字符串。

39

char[] toCharArray()

將此字符串轉換為新的字符數組。

40

String toLowerCase()

使用默認語言環境的規則將此String中的所有字符轉換為小寫。

41

String toLowerCase(Locale locale)

使用給定Locale的規則將此String中的所有字符轉換為小寫。

42

String toString()

將這個對象(已經是一個字符串)本身返回。

43

String toUpperCase()

使用默認語言環境的規則將此String中的所有字符轉換為大寫。

44

String toUpperCase(Locale locale)

使用給定Locale的規則將此String中的所有字符轉換為大寫。

45

String trim()

返回字符串的副本,移除前導和尾隨空格。

46

static String valueOf(primitive data type x)

返回傳遞的數據類型參數的字符串表示形式。

有完整的Java初級,高級對應的學習路線和資料!專注于java開發。分享java基礎、原理性知識、JavaWeb實戰、spring全家桶、設計模式、分布式及面試資料、開源項目,助力開發者成長!

歡迎關注微信公眾號:碼邦主

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393436.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393436.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393436.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android商城開發系列(二)——App啟動歡迎頁面制作

商城APP一般都會在應用啟動時有一個歡迎界面,下面我們來實現一個最簡單的歡迎頁開發:就是打開商城App,先出現歡迎界面,停留幾秒鐘,自動進入應用程序的主界面。 首先先定義WelcomeActivity布局,布局非常簡單…

DELL安裝不了mysql_Windows 版本 Mysql 8.x 安裝

1、官網下載安裝包百度網盤鏈接:https://pan.baidu.com/s/1cFRbQM5720xrzMxbgjPeyA提取碼:xlz72、解壓安裝包并新建一個文件夾作為安裝目錄(mysqlInstall)3、配置 Mysql 環境變量4、在解壓好的目錄下新建一個 my.ini 文件(注意:my.ini 文件和…

lambda 使用_如何使用Lambda和API網關構建API

lambda 使用Do you want to access your database, control your system, or execute some code from another website? An API can do all of this for you, and they’re surprisingly easy to set up.您是否要訪問數據庫,控制系統或從其他網站執行一些代碼&…

Hyper-V Server聯機調整虛擬硬盤大小

1. 技術概述: 從 Windows Server 2012 R2開始,管理員可以在運行虛擬機的同時,使用 Hyper-V 來擴展或壓縮虛擬硬盤的大小。存儲管理員可以通過對運行中的虛擬硬盤執行維護操作來避免代價不菲的停機。不再需要關閉虛擬機,這可以避免…

leetcode162. 尋找峰值(二分法)

峰值元素是指其值大于左右相鄰值的元素。 給定一個輸入數組 nums,其中 nums[i] ≠ nums[i1],找到峰值元素并返回其索引。 數組可能包含多個峰值,在這種情況下,返回任何一個峰值所在位置即可。 你可以假設 nums[-1] nums[n] -…

python網絡爬蟲(5)BeautifulSoup的使用示范

創建并顯示原始內容 其中的lxml第三方解釋器加快解析速度 import bs4 from bs4 import BeautifulSoup html_str """ <html><head><title>The Dormouses story</title></head> <body> <p class"title"><…

Mingw編譯DLib

Mingw編譯DLib 因為機器上安裝了qt-opensource-windows-x86-mingw530-5.8.0&#xff0c;所以準備使用其自帶的mingw530來編譯DLib使用。 因為DLib使用CMake的構建腳本&#xff0c;所以還請先安裝好CMake。 cmake的下載地址如下https://cmake.org/files/v3.7/cmake-3.7.2-win64-…

探索JavaScript的關閉功能

Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!“發現功能JavaScript”被BookAuthority評為最佳新功能編程書籍之一 &#xff01; A closure is an inner function that has access to the outer scope, even…

QueryList 配置curl參數 的文檔位置 QueryList抓取https 終于找到了

需要設置ssl證書&#xff0c;或者不驗證證書&#xff0c;例&#xff1a;$ql QueryList::get(https://...,[],[verify > false]);設置這個 verify > false , 所以curl的其他參數就在這里配置即可 文檔在 https://guzzle-cn.readthedocs.io/zh_CN/latest/request-optio…

leetcode981. 基于時間的鍵值存儲(treemap)

創建一個基于時間的鍵值存儲類 TimeMap&#xff0c;它支持下面兩個操作&#xff1a; set(string key, string value, int timestamp) 存儲鍵 key、值 value&#xff0c;以及給定的時間戳 timestamp。 2. get(string key, int timestamp) 返回先前調用 set(key, value, times…

物聯網筆記

轉載于:https://www.cnblogs.com/16-C-kai/p/6596682.html

關于大學生玩網絡游戲的調查問卷

1.創建問卷&#xff0c;輸入調查名稱 2編輯問卷 3檢查問卷&#xff0c;是否有誤 4.提交并發布問卷 5分享問卷 6.問卷分析 轉載于:https://www.cnblogs.com/dzw1996/p/7786754.html

java自動排序_java ArrayList自動排序算法的實現

前幾天寫的那個是錯誤的&#xff0c;在這里將正確的更新。。。通過實現ComParator接口&#xff0c;并且對Compare函數進行重寫&#xff0c;自定義排序規則實現對ArrayList中對象的排序。。Student類定義&#xff1a;通過右鍵-》source-》自動生成Set和get方法package first;imp…

1到100的二進制編碼_每天經過100天的編碼后,我學到了什么

1到100的二進制編碼Eleftheria Batsou is a web developer from Thessaloniki, Greece. She gave a talk at the Codegarden conference about her experience doing a solid 100 days of coding every day as part of the #100DaysOfCode Challenge.Eleftheria Batsou是來自希…

第六次 實驗

轉載于:https://www.cnblogs.com/P201821440005/p/10967987.html

leetcode658. 找到 K 個最接近的元素(二分法)

給定一個排序好的數組&#xff0c;兩個整數 k 和 x&#xff0c;從數組中找到最靠近 x&#xff08;兩數之差最小&#xff09;的 k 個數。返回的結果必須要是按升序排好的。如果有兩個數與 x 的差值一樣&#xff0c;優先選擇數值較小的那個數。 示例 1: 輸入: [1,2,3,4,5], k4,…

du命令、df命令用法

一、du命令 [plain] view plaincopy print?[rootwc1 mysql]# du --help Usage: du [OPTION]... [FILE]... or: du [OPTION]... --files0-fromF Summarize disk usage of each FILE, recursively for directories. Mandatory arguments to long options are mandatory…

mysql 循環創建列_mysql – 查詢列中的循環值

我需要創建一個查詢,一次只將一列的值移動一行↑&#xff1a;----------------------------| anotherCOL | values_to_loop |----------------------------| 1 | 1 || 2 | 2 || 3 | 3 || 4 | 4 || 5 | 5 || 6 | 6 || 7 | 7 || 8 | 8 || 9 | 9 || 10 | 10 |--------------------…

因子個數與因子和

題目&#xff1a;LightOJ:1341 - Aladdin and the Flying Carpet(因子個數&#xff09; Its said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was …

如何在JavaScript中直觀地設計狀態

by Shawn McKay肖恩麥凱(Shawn McKay) 如何在JavaScript中直觀地設計狀態 (How to visually design state in JavaScript) 使用狀態機和狀態圖開發應用程序的路線圖 (A roadmap for developing applications with state machines & statecharts) Why does state managemen…