Java 中 String 的常用方法(二)

本文介绍剩下的一些常用的 String 中的方法。

1、replace 方法 、replaceFirst 方法和 replaceAll 方法

replace(char oldChar, char newChar)
Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.

replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.

这几个方法都是用指定的字符或字符串将原有的字符串中的对应内容进行替换,前面两个不支持正则表达式,后边两个支持正则

String x = "[hello\\google\\bye]";
System.out.println(x.replace('\\', '/'));
System.out.println(x.replace("oo", "++"));
System.out.println(x.replace("\\", "++"));
System.out.println(x.replaceFirst("\\\\", "/"));
System.out.println(x.replaceAll("oo", "++"));
System.out.println(x.replaceAll("\\\\", "++"));

输出结果为:

[hello/google/bye]
[hello\g++gle\bye]
[hello++google++bye]
[hello/google\bye]
[hello\g++gle\bye]
[hello++google++bye]

根据测试 replaceAll 函数要更快一些。看源码发现,replace 函数里面仍使用 replaceAll 函数。

总体原则:当字符串无法确定是否具有转义字符时,而且也不需要转义时,建议使用 replace 函数;否则,使用 replaceAll 函数。

PS:正则表达式中匹配一个反斜杠为何需要用四个反斜杠?

分析一下字符串 "\\\\",第一个和第三个为转义符,第二个和第四个为斜杠本身。

字符串中表示斜杠就需要两个斜杠 "\\",

正则表达式里边的斜杠则需要转义,用 "\\" 表示,

所以我们先要表示正则表达式里边的斜杠 "\\",然后在用字符串表示出来,而这 2 个斜杠分别需要一个转义符,这样就成了 4 个斜杠在正则表达式中表示一个斜杠。

2、matches 方法

matches(String regex)
Tells whether or not this string matches the given regular expression.

该方法用来判断这个字符串是否匹配给定的正则表达式,符合则返回 true,反之则返回 false。

3、split 方法

split(String regex)
Splits this string around matches of the given regular expression.

split(String regex, int limit)
Splits this string around matches of the given regular expression.

字符串分割的方法,返回值为一个 String 类型的数组

第一个参数 int limit 可以限制进行正则匹配的次数,

如果 limit > 0,则进行正则匹配的次数为 limit - 1 次,

如果 limit < 0,则不限制匹配的次数,

如果 limit = 0,则不限制匹配的次数,并且分隔之后的数组结尾的空字符串被忽略。

String str = "boo:and:foo";
System.out.println(Arrays.toString(str.split(":", 2)));
System.out.println(Arrays.toString(str.split(":", 5)));
System.out.println(Arrays.toString(str.split(":", -2)));
System.out.println(Arrays.toString(str.split("o", 5)));
System.out.println(Arrays.toString(str.split("o", -2)));
System.out.println(Arrays.toString(str.split("o", 0)));

输出结果为:

[boo, and:foo]
[boo, and, foo]
[boo, and, foo]
[b, , :and:f, , ]
[b, , :and:f, , ]
[b, , :and:f]

4、toLowerCase 方法和 toUpperCase 方法

toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default locale.

toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale.

大小写转换方法

String str = "hello google";
System.out.println(str.toUpperCase());
String str1 = "HELLO google";
System.out.println(str1.toLowerCase());

输出结果为:

HELLO GOOGLE
hello google

5、trim 方法

trim()
Returns a string whose value is this string, with any leading and trailing whitespace removed.

去掉给定字符串前后的空白

String str = "       hello, google              ";
System.out.println(str.trim());

输出结果:

hello, google

6、valueOf 方法

将其他类型的对象转换为字符串,包括了 boolean,char,char[],double,float,int,long 以及 Object 类型。

返回对应字符串或者 null。

转载于:https://www.cnblogs.com/JavaSubin/p/5492173.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/255311.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【adb错误修复】adb version(39) doesn‘t match the client(40),killing...

问题原因&#xff1a; 由于服务端的adb版本和客户端的adb版本不一样【哪个是客户端哪个是服务端我也不清楚】 解决方法&#xff1a; adb kill-serveradb start-server

HALCON示例程序ball电路板焊点识别、检测、测量程序2剖析(与上篇文章使用了不同方法)

HALCON示例程序ball电路板焊点识别、检测、测量程序2 示例程序源码&#xff08;加注释&#xff09; *这是关于显示的函数&#xff0c;已经介绍过了 dev_update_off () *定义一个字符串变量ImageNames&#xff0c;ImageNames[0]的含义为’die/die_02’以此类推 ImageNames : ‘…

arduino 程序的机制

从一个简单的 arduino 程序说起&#xff1a; /*BlinkTurns on an LED on for one second, then off for one second, repeatedly.This example code is in the public domain.*/// Pin 13 has an LED connected on most Arduino boards. // give it a name: int led 13;// the…

电池技术多年没有较大发展,成为移动设备最大制约

各大厂商致力于发展闪充技术以此解决电池续航问题。 对于电子产品来说&#xff0c;不管是什么&#xff0c;我们最头疼的无疑就是电池问题了。而电池的重要性更是不用多说&#xff0c;但是&#xff0c;以目前的科技水平来看&#xff0c;就拿智能手机的CPU等功能来说&#xff0c…

什么样的数据应该放入缓存

把数据放入缓存,有三个标准: 1.数据量不大 2.访问频率高 3.数据更改频率低转载于:https://www.cnblogs.com/hwgok/p/5494915.html

VMWare 安装 Eclipse

由于之前已经安装了 OpenJDK 所以 这次我们可以直接下载 eclipse来安装。 Eclipse 下载&#xff1a;http://www.eclipse.org/downloads/?osTypelinux&releaseundefined 如果下载后 在 下载目录&#xff0c;运行解压 &#xff08;我下载的&#xff1a;eclipse-inst-lin…

C语言条件编译及编译预处理阶段

一、C语言由源代码生成的各阶段如下&#xff1a; C源程序&#xff0d;>编译预处理&#xff0d;>编译&#xff0d;>优化程序&#xff0d;>汇编程序&#xff0d;>链接程序&#xff0d;>可执行文件其中 编译预处理阶段&#xff0c;读取c源程序&#xff0c;对其中…

HALCON示例程序board.hdev检测电路板焊锡有无程序剖析

HALCON示例程序board.hdev检测电路板焊锡有无程序剖析 示例程序源码&#xff08;加注释&#xff09; *这是关于系统设置的函数&#xff0c;剪辑区域&#xff0c;设置剪辑区域设置为使能。为clip_region做的设置&#xff0c;后文会介绍 get_system (‘clip_region’, Informat…

【机器学习】SVM理论与python实践系列

理论部分: 《机器学习——支持向量机SVM之线性模型》 《机器学习——支持向量机SVM之非线性模型低维到高维映射》 《机器学习——支持向量机SVM之非线性模型原问题与对偶问题》 《机器学习——常用核函数》 《机器学习——支持向量机SVM之非线性模型原问题转化为对偶问题…

eoLinker-API_Shop_验证码识别与生成类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等...

以下示例代码适用于 www.apishop.net 网站下的API&#xff0c;使用本文提及的接口调用代码示例前&#xff0c;您需要先申请相应的API服务。 六位图片验证码生成&#xff1a;包括纯数字、小写字母、大写字母、大小写混合、数字小写、数字大写、数字大小写等情况。四位图片验证码…

网上书店 买方数据库

买方表 属性 字段名 类型 键值 是否空 用户ID UserId char(5) 主键 用户名称 UserName nvarchar(50) 用户密码 UserPwd nvarchar(50) 用户真实姓名 UserRealName nvarchar(50) 用户地址 UserAddress nvarchar(100) …

Web开发模式(MVC设计模式)

1.MVC&#xff1a;(Model-View-Controller)操作流程 显示层View:主要负责接收Servlet传递的内容&#xff0c;并调用JavaBean把内容显示给用户。 控制层Controller:负责所有的用户请求参数&#xff0c;判断请求参数是否合法&#xff0c;根据请求方式调用JavaBean进行处理&#x…

Arduino IDE 配置文件

最近学习Arduino。 Arduino开源硬件和Arduino IDE是一个很容易上手的系统。 目前arduino已经支持很多种板类型&#xff0c;甚至已经支持了部分arm芯片。比如arduino ng、arduino uno、arduino mini、pro mini等。但是大多数情况&#xff0c;都是使用的atmega8/at…

HALCON示例程序bottle.hdev、bottlet.hdev瓶体字符OCR的训练和检测

HALCON示例程序bottle.hdev、bottlet.hdev瓶体字符OCR的训练和检测 示例程序源码&#xff08;加注释&#xff09; 1、先介绍bottlet.hdev&#xff08;训练OCR识别文件&#xff09; *定义一个字符串变量FontName &#xff0c;内容是bottle FontName : ‘bottle’ *第一步分割…

【机器学习】神经网络BP理论与python实例系列

理论部分 《机器学习——人工神经网络之发展历史&#xff08;神经元数学模型、感知器算法&#xff09;》 《机器学习——人工神经网络之多层神经网络&#xff08;多层与三层&#xff09;》 《机器学习——人工神经网络之后向传播算法&#xff08;BP算法&#xff09;》 《机…

bzoj1597: [Usaco2008 Mar]土地购买

斜率优化dp。 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn 50000 10;struct Field {long long x,y; } t[maxn]; long long f[maxn],x[maxn],y[maxn]; int q[maxn],l,r; int n,cnt;bool cmp(F…

vue封装axios接口

一、安装axios axios安装命令&#xff1a;cnpm install axios 二、在文件中引用axios 一开始我是放在src下的main.js这个文件里面&#xff0c;后来发现mounted钩子读取接口方法为undefined&#xff0c;百度了才发现是vue生命周期的原因&#xff0c;最好的解决办法是把axios单独…

编写Arduino支持的C++类库

以下为摘抄的例子&#xff0c;已经亲自验证过&#xff0c;例子是正确的 我们在上一讲中实现了一个TN901红外温度传感器51程序到Arduino程序的转换&#xff0c;如果代码越来越多这样程序的可维护性会随之降低&#xff0c;也不适合团度开发。我们应该把常用的文件封装成C库&#…

函数,游标与存储过程的综合应用

--在TOY数据库中完成以下操作use toygo--1、编写一个存储过程&#xff0c;接收任意一个订单号&#xff0c;打印订单的表头数据。格式如下&#xff1a;--(其中订单编号由订单日期订单号的字符串组成)--订单编号:20010520000001 订货日期:2001-05-20 订货人:拉尔森create pro…

HALCON示例程序check_blister.hdev药品胶囊检测

HALCON check_blister.hdev药品胶囊检测 示例程序源码&#xff08;加注释&#xff09; 显示、读入图片、设置显示字体等&#xff0c;之前的帖子已经介绍过了 dev_close_window () dev_update_off () read_image (ImageOrig, ‘blister/blister_reference’) dev_open_window_f…