百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

Java String 常用方法,涵盖全部

toyiye 2024-06-21 12:27 8 浏览 0 评论

1、[/size]

[size=5]

[/size]

[size=5]String类常用方法


2

1.2、求字符串某一位置字符

public char charAt(int index)//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。


String str = new String("asdfzxc");

char ch = str.charAt(4);//ch = z12

1

2

1.3、提取子串

用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:

1)public String substring(int beginIndex)//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。

2)public String substring(int beginIndex, int endIndex)//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。


String str1 = new String("asdfzxc");

String str2 = str1.substring(2);//str2 = "dfzxc"

String str3 = str1.substring(2,5);//str3 = "dfz" 123

1

2

3

1.4、字符串比较

1)public int compareTo(String anotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。

2)public int compareToIgnore(String anotherString)//与compareTo方法相似,但忽略大小写。

3)public boolean equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。

4)public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。


String str1 = new String("abc");

String str2 = new String("ABC");

int a = str1.compareTo(str2);//a>0

int b = str1.compareTo(str2);//b=0

boolean c = str1.equals(str2);//c=false

boolean d = str1.equalsIgnoreCase(str2);//d=true123456

1

2


1.5、字符串连接

public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于”+”。


String str = "aa".concat("bb").concat("cc"); 1

1

相当于String str = “aa”+”bb”+”cc”;


1.6、字符串中单个字符查找

1)public int indexOf(int ch/String str)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。

2)public int indexOf(int ch/String str, int fromIndex)//方法与第一种类似,区别在于该方法从fromIndex位置向后查找。

3)public int lastIndexOf(int ch/String str)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。

4)public int lastIndexOf(int ch/String str, int fromIndex)//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。


String str = "I am a good student";

int a = str.indexOf('a');//a = 2

int b = str.indexOf("good");//b = 7

int c = str.indexOf("w",2);//c = -1

int d = str.lastIndexOf("a");//d = 5

int e = str.lastIndexOf("a",3);//e = 2123456

1

2

3

1.7、字符串中字符的大小写转换

1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串

2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串


String str = new String("asDF");

String str1 = str.toLowerCase();//str1 = "asdf"

String str2 = str.toUpperCase();//str2 = "ASDF" 123

1

2

3

1.8、字符串中字符的替换

1)public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。

2)public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。

3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。


String str = "asdzxcasd";

String str1 = str.replace('a','g');//str1 = "gsdzxcgsd"

String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"

String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"

String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh" 12345

1

2

3

String str = " a sd ";

String str1 = str.trim();

int a = str.length();//a = 6

int b = str1.length();//b = 4 1234

1

2

3

4

//用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。

2)boolean statWith(String prefix)或boolean endWith(String suffix)


String str = "asdfgh";

boolean a = str.statWith("as");//a = true

boolean b = str.endWith("gh");//b = true 123

1

2

3//从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。

3)regionMatches(boolean b, int firstStart, String other, int otherStart, int length)

4)contains(String str)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。


String str = "student";

str.contains("stu");//true

str.contains("ok");//false 123

1

2

3

5)String[] split(String str)//将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。

String str = "asd!qwe|zxc#";

String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc"; 12

1

2

3

indexOf(String str):获取str在字符串对象中第一次出现的索引

String str = "adsfaxsdfas沙发上案发地方";

System.out.println(str.indexOf('a',5));

1

2

运行结果:

94)substring(int start):从start开始截取字符串

String str = "adsfaxsdfas沙发上案发地方";

System.out.println(str.substring(1));

1

2

运行结果:

dsfaxsdfas沙发上发地方


5)String substring(int start,int end):从start开始,到end结束截取字符串。包括start,不包括

1endString str = "adsfaxsdfas沙发上案发地方";

System.out.println(str.substring(1, 12));

2

运行结果:

dsfaxsdfas沙


String判断功能1)[/size]

[size=5]

equals(Object obj):比较字符串的内容是否相同

String str = "adsfaxsdfas沙发上案发地方";

1System.out.println(str.equals("adsfaxsdfas沙发上发地方"));

2System.out.println(str.equals("adsfaxsdfas"));

3

运行结果:

true

false2)


equalsIgnoreCase(String anotherString):比较字符串的内容是否相同,忽略大小写

1String str = "adsfaxsdfas沙发上案发地方";

2System.out.println(str.equalsIgnoreCase("ADsfaxsdfAs沙发上发地方"));

运行结果:

true3)


startsWith(String prefix):判断字符串对象是否以指定的字符开头(区分大小写)

2String str = "adsfaxsdfas沙发上案发地方";

System.out.println(str.startsWith("a"));

System.out.println(str.startsWith("A"));

1

3

运行结果:

true

false4)


startsWith(String prefix,int toffset):判断字符串对象是否以指定的字符开头,参数toffset为指定从哪个下标开始

String str = "adsfaxsdfas沙发上案发地方";

System.out.println(str.startsWith("f", 3));

System.out.println(str.startsWith("f", 4));

1

2

3

运行结果:

true

false5)


endsWith(String str):判断字符串对象是否以指定的字符结尾

String str = "adsfaxsdfas沙发上案发地方";

System.out.println(str.endsWith("x"));

System.out.println(str.endsWith("方"));

1

2

3

运行结果:

false

true6)


isEmpty():判断指定字符串是否为空7)startsWith(String prefix):判断字符串是否以指定字符开头

String str = "adsfaxsdfas沙发上发地方";

System.out.println(str.startsWith("a"));

1

2

运行结果:

true4.


toLowerCase():把字符串转换为小写字符串

String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

1System.out.println(str1.toLowerCase());

2

运行结果:

abcdefghijklmnopqrstuvwxyz3)


toUpperCase():把字符串转换为大写字符串

String str1 = "abcdefghijklmnopqrstuvwxyz";

System.out.println(str2.toUpperCase());

1

2

运行结果:

ABCDEFGHIJKLMNOPQRSTUVWXYZ5.


其他常用方法1)trim():去除字符串两端空格

String str3 = " a c e x u a n x u a n ";

1

2System.out.println(str3.trim());System.out.println(str3);

3运行结果:a c e x u a n x u a n a c e x u a n x u a n


6

7

2)split():去除字符串中指定的的字符,然后返回一个新的字符串

public static void main(String[ args) {

String str = "adsfaxsdfas沙发上发地方";

String array[ = str.split("a");

printString(array);

}

public static void printString(String a[) {

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

System.out.print(a[i); }

}

1

2运行结果:

dsfxsdfs沙发上发地方


3)subSequence(int beginIndex,int endIndex ):截取字符串中指定位置的字符组成一个新的字符串

String str = "adsfaxsdfas沙发上发地方";

System.out.println(str.subSequence(1, 10));

1

2

运行结果:

dsfaxsdfa


4)replace(char oldChar, char newChar):将指定字符替换成另一个指定的字符

String str = "adsfaxsdfas沙发上发地方";

System.out.println(str.replace('a', 's'));

1

2

运行结果:

sdsfsxsdfss沙发上发地方


5)replaceAll(String regex,String replasement):用新的内容替换全部旧内容

String str4 = "Hello,world!";

System.out.println(str4.replaceAll("l", "&"));

1

2

运行结果:

He&&o,wor&d!


6)replaceFirst(String regex,String replacement):替换首个满足条件的内容

String str = "adsfaxsdfas沙发上发地方";

System.out.println(str.replaceFirst("沙", "璇"));

1

2

运行结果:

adsfaxsdfas璇发上发地方


7)lastIndexOf(String str):返回指定字符出现的最后一次的下标

String str4 = "Hello,world!";

System.out.println(str4.lastIndexOf("l"));

1

2

运行结果:

9


8)contains(CharSequence s):查看字符串中是都含有指定字符

String str4 = "Hello,world!";

System.out.println(str4.contains("l"));

1

2运行结果:

true

[/size]

[size=5][b]9)concat(String str):在原有的字符串的基础上加上指定字符串[/b][/size]

[size=5]String str5 [color=rgb(98, 114, 164)]=[/color] [color=rgb(241, 250, 140)]"dr"[/color][color=rgb(153, 153, 153)];[/color][/size]

[size=5]System[color=rgb(153, 153, 153)].[/color]out[color=rgb(153, 153, 153)].[/color][color=rgb(139, 233, 253)]println[/color][color=rgb(153, 153, 153)]([/color]str5[color=rgb(153, 153, 153)].[/color][color=rgb(139, 233, 253)]concat[/color][color=rgb(153, 153, 153)]([/color][color=rgb(241, 250, 140)]"eam"[/color][color=rgb(153, 153, 153)])[/color][color=rgb(153, 153, 153)])[/color][color=rgb(153, 153, 153)];[/color][/size]

[size=5]

[/size]

[size=5]2.2、基本类型转换为字符串类型

String类中提供了String valueOf()放法,用作基本类型转换为字符串类型。

1)static String valueOf(char data[])

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

3)static String valueOf(boolean b)

4)static String valueOf(char c)

5)static String valueOf(int i)

6)static String valueOf(long l)

7)static String valueOf(float f)

8)static String valueOf(double d)

例如:


String s1 = String.valueOf(12);

String s1 = String.valueOf(12.34);

相关推荐

为何越来越多的编程语言使用JSON(为什么编程)

JSON是JavascriptObjectNotation的缩写,意思是Javascript对象表示法,是一种易于人类阅读和对编程友好的文本数据传递方法,是JavaScript语言规范定义的一个子...

何时在数据库中使用 JSON(数据库用json格式存储)

在本文中,您将了解何时应考虑将JSON数据类型添加到表中以及何时应避免使用它们。每天?分享?最新?软件?开发?,Devops,敏捷?,测试?以及?项目?管理?最新?,最热门?的?文章?,每天?花?...

MySQL 从零开始:05 数据类型(mysql数据类型有哪些,并举例)

前面的讲解中已经接触到了表的创建,表的创建是对字段的声明,比如:上述语句声明了字段的名称、类型、所占空间、默认值和是否可以为空等信息。其中的int、varchar、char和decimal都...

JSON对象花样进阶(json格式对象)

一、引言在现代Web开发中,JSON(JavaScriptObjectNotation)已经成为数据交换的标准格式。无论是从前端向后端发送数据,还是从后端接收数据,JSON都是不可或缺的一部分。...

深入理解 JSON 和 Form-data(json和formdata提交区别)

在讨论现代网络开发与API设计的语境下,理解客户端和服务器间如何有效且可靠地交换数据变得尤为关键。这里,特别值得关注的是两种主流数据格式:...

JSON 语法(json 语法 priority)

JSON语法是JavaScript语法的子集。JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组JS...

JSON语法详解(json的语法规则)

JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔大括号保存对象中括号保存数组注意:json的key是字符串,且必须是双引号,不能是单引号...

MySQL JSON数据类型操作(mysql的json)

概述mysql自5.7.8版本开始,就支持了json结构的数据存储和查询,这表明了mysql也在不断的学习和增加nosql数据库的有点。但mysql毕竟是关系型数据库,在处理json这种非结构化的数据...

JSON的数据模式(json数据格式示例)

像XML模式一样,JSON数据格式也有Schema,这是一个基于JSON格式的规范。JSON模式也以JSON格式编写。它用于验证JSON数据。JSON模式示例以下代码显示了基本的JSON模式。{"...

前端学习——JSON格式详解(后端json格式)

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgrammingLa...

什么是 JSON:详解 JSON 及其优势(什么叫json)

现在程序员还有谁不知道JSON吗?无论对于前端还是后端,JSON都是一种常见的数据格式。那么JSON到底是什么呢?JSON的定义...

PostgreSQL JSON 类型:处理结构化数据

PostgreSQL提供JSON类型,以存储结构化数据。JSON是一种开放的数据格式,可用于存储各种类型的值。什么是JSON类型?JSON类型表示JSON(JavaScriptO...

JavaScript:JSON、三种包装类(javascript 包)

JOSN:我们希望可以将一个对象在不同的语言中进行传递,以达到通信的目的,最佳方式就是将一个对象转换为字符串的形式JSON(JavaScriptObjectNotation)-JS的对象表示法...

Python数据分析 只要1分钟 教你玩转JSON 全程干货

Json简介:Json,全名JavaScriptObjectNotation,JSON(JavaScriptObjectNotation(记号、标记))是一种轻量级的数据交换格式。它基于J...

比较一下JSON与XML两种数据格式?(json和xml哪个好)

JSON(JavaScriptObjectNotation)和XML(eXtensibleMarkupLanguage)是在日常开发中比较常用的两种数据格式,它们主要的作用就是用来进行数据的传...

取消回复欢迎 发表评论:

请填写验证码