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.mzph.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…