博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中String类的常用功能以及方法
阅读量:2039 次
发布时间:2019-04-28

本文共 4590 字,大约阅读时间需要 15 分钟。

java中String类的常用功能以及方法

一. 构造方法:

1. String(String original);

功能:把字符串数据封装成字符串对象;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s = new String("helloworld");		System.out.println(s);	}}

注意:字符串是一种比较特殊的引用数据类型,直接输出字符串对象输出的是该对象中的数据。

2. String(char[] value);

功能:把字符数组的数据封装成字符串对象

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		char[] str = {'h','e','l','l','o'};		String s = new String(str);		System.out.println(s);	}}

3. String(char[] value, int index, int count);

功能:把字符数组中的一部分数据封装成字符串对象,即:将字符数组中以索引为index开头以及后面的数据,共count个数据封装成字符串对象;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		char[] str = {'h','e','l','l','o'};		String s = new String(str,1,3);		System.out.println(s);	}}

在这里插入图片描述

二. String类的判断功能:

1. boolean equals(Object obj);

功能:比较字符串的内容是否相同;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s1 = "hello";		String s2 = "hello";		String s3 = "Hello";		System.out.println(s1.equals(s2));		System.out.println(s1.equals(s3));	}}

在这里插入图片描述

2. boolean equalsIgnoreCase(String str);

功能:比较字符串的内容是否相同,忽略大小写;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s1 = "hello";		String s2 = "hello";		String s3 = "HELLO";		String s4 = "helll";		System.out.println(s1.equalsIgnoreCase(s2));		System.out.println(s1.equalsIgnoreCase(s3));		System.out.println(s1.equalsIgnoreCase(s4));	}}

3. boolean startsWith(String str);

功能:判断字符串对象是否以指定的str开头;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s1 = "hello";		String s2 = "he";		String s3 = "el";		System.out.println(s1.startsWith(s2));		System.out.println(s1.startsWith(s3));	}}

4. boolean endsWith(String str);

功能:判断字符串对象是否以指定的str结尾;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s1 = "hello";		String s2 = "llo";		String s3 = "ll";		System.out.println(s1.endsWith(s2));		System.out.println(s1.endsWith(s3));	}}

三. String类的获取功能:

1. int length();

功能:获取字符串的长度,即字符个数;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s1 = "hello";		System.out.println(s1.length());	}}

2. char charAt(int index);

功能:获取指定索引处的字符;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s1 = "hello";		for(int i=0;i

3. int indexOf(String str);

功能:获取str在字符串对象中第一次出现的索引;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s = "hello";		System.out.println(s.indexOf("h"));		System.out.println(s.indexOf("e"));		System.out.println(s.indexOf("l"));		System.out.println(s.indexOf("l"));	}}

在这里插入图片描述

4. String substring(int start);

功能:从start开始截取字符串;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s = "hello";		System.out.println(s.substring(1));		System.out.println(s.substring(3));	}}

5. String substring(int start,int end);

功能:从start开始,到end结束截取字符串。包括start,不包括end

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s = "hello";		System.out.println(s.substring(1,2));		System.out.println(s.substring(3,s.length()));	}}

四. String类的转换功能:

1. char[] toCharArray();

功能:把字符串转换为字符数组;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s = "hello";		char[]  arr = s.toCharArray();		System.out.println(arr);		for(int i=0;i

在这里插入图片描述

2. String toLowerCase();

功能:把字符串转换为小写字符串;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s1 = "HelloWorld";		String s2 = s1.toLowerCase();		System.out.println(s1);		System.out.println(s2);	}}

3. String toUpperCase();

功能:把字符串转换为大写字符串;

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s1 = "HelloWorld";		String s2 = s1.toUpperCase();		System.out.println(s1);		System.out.println(s2);	}}

五. 去除字符串两端空格 :

1. String trim();

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s1 = "     Hel lo  World    ";		String s2 = s1.trim();		System.out.println("----"+s1+"----");		System.out.println("----"+s2+"----");	}}

六. 按照指定符号分割字符串:

1. String[] split(String str);

示例:

package string.demo_5;public class test03 {	public static void main(String[] args) {		String s = "12,34,56,78";		String[] arr = s.split(",");		for(int i=0;i

注意:使用"|“或者”.“去分割某字符时,因 “|“或者”.“本身是正则表达式中的一部分,所以需要” \” 去转义,因转义使用”\", 而这个"\“正好也是正则表达式的字符,所以还得用一个”\", 所以需要两个即:"\\"。
所以使用时应该写为"\\.“或者”\\|"。

转载地址:http://hgyof.baihongyu.com/

你可能感兴趣的文章
Spring总结之注解(2)
查看>>
Maven常用命令大全与pom文件讲解
查看>>
Java和JavaScript中使用Json方法大全
查看>>
Ubuntu14.04下安装docker
查看>>
ubuntu下安装nginx
查看>>
Linux 更改文件名
查看>>
Linux下安装Elasticsearch5.X
查看>>
linux命令ps aux|grep xxx详解
查看>>
MySQL常见问题
查看>>
Spring Boot 入门之缓存和 NoSQL 篇(四)
查看>>
使用Docker高效搭建开发环境
查看>>
微服务下的数据架构
查看>>
Nginx 容器教程
查看>>
linux下的命令: sudo ln -s 源文件 目标文件
查看>>
关于 Mybatis mapping.xml中的 StatementType 知识点
查看>>
小议“悲观锁和乐观锁”的原理、场景、示例
查看>>
面试中的这些坑,你踩过几个?
查看>>
socket,tcp,http三者之间的区别和原理
查看>>
Spring AOP 最热门面试题及答案
查看>>
Union和Union All到底有什么区别
查看>>