Java字符串的操作
判断字符串是否存在
使用str.contains("values")
public class one {
/*判断某个字符串是否存在*/
public static void main(String[] args){
String str = "今晚打老虎";
boolean request = str.contains("老虎");
System.out.println("今晚有老虎吗?");
if (request == true) {
System.out.println("今晚有老虎");
}else {
System.out.println("今晚没老虎");
}
}
结果:今晚有老虎吗?
今晚有老虎
字符串截取
public class one {
/**字符串截取*/
public static void main(String[] args){
String str = "123456201808086789";
String year = str.substring(6,14);//也可以只输入一个开始索引位置
System.out.println(year);
}
结果:20180808
字符串替换
public class one {
/**字符串替换*/
public static void main(String[] args){
String str = "娃嘿嘿";
String restr = str.replace("嘿","哈");//将字符串中所以得"嘿"替换成"哈"
System.out.println(restr);
}
结果:娃哈哈
字符串分割
/**字符串分割*/
public static void main(String[] args){
String str = "美的,海尔,格力";
String new_list[] = str.split(",");//以","号分割
for (int i = 0;i < new_list.length;i++) {
System.out.println("第" + i + "个是" + new_list[i]);//取list的第i个
}
}
结果:
第0个是美的
第1个是海尔
第2个是格力
转换字符串大小写
/**字符串大小写转换*/
public static void main(String[] args){
String str = "abCD";
System.out.println(str.toLowerCase());//将字符串转换为小写
System.out.println(str.toUpperCase());//将字符串转换为大写
}
结果:
abcd
ABCD
去除字符串空格
/**字符串去空格*/
public static void main(String[] args){
String str = " abCD ";
System.out.println(str.trim());//去除空格
}
结果:abCD
判断字符串是否相等
/**判断字符串是否相等*/
public static void main(String[] args){
String str1 = "Hello";
String str2 = "你好";
String str3 = "Hello";
System.out.println(str1 == str3);//使用==判断是否相等
System.out.println(str1 == str2);
System.out.println("华丽的分割线<------------------------------------------>");
System.out.println(str1.equals(str3));//使用equals判断是否相等
System.out.println(str1.equals(str2));
}
结果:
true
false
华丽的分割线<------------------------------------------>
true
false
true
Java字符串的操作的更多相关文章
- JAVA字符串拼接操作规则说明
1.常量与常量的拼接结果在常量池,原理是编译期优化 public void test1() { String s1 = "a" + "b" + "c& ...
- 【转】Java 字符串常用操作(String类)
原文网址:http://www.cnblogs.com/freeabyss/archive/2013/05/15/3187057.html 字符串查找 String提供了两种查找字符串的方法,即ind ...
- Java 字符串常用操作(String类)
字符串查找 String提供了两种查找字符串的方法,即indexOf与lastIndexOf方法. 1.indexOf(String s) 该方法用于返回参数字符串s在指定字符串中首次出现的索引位置, ...
- Java字符串连接操作的性能问题
首先,看一段实验程序: package com.test; class StringTest { public static void main(String[] args) { long start ...
- learning java 字符串常用操作
// 字符串索引取值 "; System.)); // 字符串比较 "; "; "; System.out.println(s1.compareTo(s2)); ...
- 1 通过JNI混合使用Java和C++ -----> 操作字符串
JNI(Java Native Interface)是Java语言的一部分,可以访问非Java语言编写的程序,也可以用于在C++程序中执行Java代码. 步骤: 1> 编写带有native声明 ...
- java常见字符串的操作
/** * java常见字符串的操作 */ public class Test7 { public static void main(String args[]){ StringBuffer sBuf ...
- JAVA字符串操作 (转)
JAVA字符串操作 原帖地址:http://blog.163.com/hn_myj@126/blog/static/50555635200861133942947/ 参考:http://blog.cs ...
- Java字符串操作及与C#字符串操作的不同
每种语言都会有字符串的操作,因为字符串是我们平常开发使用频率最高的一种类型.今天我们来聊一下Java的字符串操作及在某些具体方法中与C#的不同,对于需要熟悉多种语言的人来说,作为一种参考.进行诫勉 首 ...
随机推荐
- SSD详解
This results in a significant improvement in speed for high-accuracy detection(59 FPS with mAP 74.3% ...
- check nginx配置文件错误:[emerg]: getpwnam(“nginx”) failed
1.错误提示: [root@server include]# /application/nginx/sbin/nginx -t -c /applications/nginx/nginx/nginx.c ...
- 愉快且方便的处理时间-- LocalDate
java中做时间处理时一般会采用java.util.Date,但是相比于Date来说,还有更好的选择 -- java.time.LocalDate. 这是jdk8中新增的日期处理类,同时新增的还有ja ...
- Math.random()和UUID.randomUUID().toString()性能对比【纯原】
Math.random()和UUID.randomUUID().toString()性能对比 不言而喻,因为Math.random()不需要保证唯一性,所做的操作远比UUID消耗更小的性能, 在部分要 ...
- search 重要文件路径 搜索【原】
hosts文件路径 C:/WINDOWS/system32/drivers/etc/ tnsnames.ora文件路径 C:/oraclexe/app/oracle/product/11.2.0/se ...
- python遍历数组获取下标
代码 通过枚举实现 a = [,,,,,] for i,j in enumerate(a): print i,j 结果
- Http请求中 content-type 和 dataType 区别
contentType: 告诉服务器,我要发什么类型的数据 dataType:告诉服务器,我要想什么类型的数据,如果没有指定,那么会自动推断是返回 XML,还是JSON,还是script,还是Stri ...
- 六道JavaScript测验题
1.找出数字数组中最大的元素(使用Match.max函数) var a=[123,23432,345,3,34]; console.log(Math.max.apply(null,a)); 2.转化一 ...
- .net 重新注册
今天同事问 一个IIS 的监控站点 .net 出现问题:对于windows 一般都停留在重启生效思想:然并没有生效: 于是建议重新注册.NET : 一般出现原因: 在默认安装路径 重启注册: 默认的安 ...
- Centos7编译hadoop异常:Received fatal alert: handshake_failure
保持网络畅通 或者 配置代理 能够访问cdh的仓库 https://repository.cloudera.com/artifactory/cloudera-repos/ 编译hadoop版本 had ...