当前位置:网站首页>String类的常用方法
String类的常用方法
2022-06-27 19:20:00 【continueLR】
String类的21个常用方法
目录
和位置有关的方法3种:
chatAt():得到指定下标的对应字符
indexOf():得到指定内容第一次出现的下标
lastIndexOf():得到指定内容最后一次出现的下标
和长度有关的方法1种
length():得到一个字符串的字符个数
和判断有关的方法5种
equals():判断两个字符串是否相等
equalsIgnoreCase():忽略大小写判断两个字符串是否相等
contains():判断前面字符串是否包含后面字符串
startsWith():判断某个字符串是否以某个子字符串开始。
endWith():判断某个字符串是否以某个子字符串结束
和数组有关的方法3种
getBytes():将字符串对象转换成字节数组
toCharArray():将字符串转换成char数组
split():拆分字符串
和改变内容有关的方法8种
toUpperCase():全部替换为大写
toLowerCase():全部替换为小写
replace():替换指定内容
replaceAll():替换指定内容,支持正则
replaceFirst();替换指定内容(第一次出现的某内容)
substring():截取字符串
isEmpty():判断某个字符串是否为“空字符串”。底层源代码调用的应该是字符串的length()方法。
trim():去除字符串前后空白
非字符串转化为字符串1种
valueOf():静态方法,“非字符串”转换成“字符串”,
// String类当中常用方法。
//1(掌握).char charAt(int index)
char c = "中国人".charAt(1); // "中国人"是一个字符串String对象。只要是对象就能“点.”
System.out.println(c); // 国
// 2(掌握).int indexOf(String str)
// 判断某个子字符串在当前字符串中第一次出现处的索引(下标)。
System.out.println("oraclejavac++.netc#phppythonjavaoraclec++".indexOf("java")); // 6
// 3(掌握).int lastIndexOf(String str)
// 判断某个子字符串在当前字符串中最后一次出现的索引(下标)
System.out.println("oraclejavac++javac#phpjavapython".lastIndexOf("java")); //22
// 4(掌握). int length()
// 面试题:判断数组长度和判断字符串长度不一样
// 判断数组长度是length属性,判断字符串长度是length()方法。
System.out.println("abc".length()); // 3
System.out.println("".length()); // 0
// 5(掌握).boolean equals(Object anObject)
// 比较两个字符串必须使用equals方法,不能使用“==”
// equals方法有没有调用compareTo方法? 老版本可以看一下。JDK13中并没有调用compareTo()方法。
// equals只能看出相等不相等。
// compareTo方法可以看出是否相等,并且同时还可以看出谁大谁小。
System.out.println("abc".equals("abc")); // true
// 6(掌握).boolean equalsIgnoreCase(String anotherString)
// 判断两个字符串是否相等,并且同时忽略大小写。
System.out.println("ABc".equalsIgnoreCase("abC")); // true
// 7(掌握).boolean contains(CharSequence s)
// 判断前面的字符串中是否包含后面的子字符串。
System.out.println("HelloWorld.java".contains(".java")); // true
System.out.println("http://www.baidu.com".contains("https://")); // false
// 8(掌握)、boolean startsWith(String prefix)
// 判断某个字符串是否以某个子字符串开始。
System.out.println("http://www.baidu.com".startsWith("http")); // true
System.out.println("http://www.baidu.com".startsWith("https")); // false
// 9(掌握). boolean endsWith(String suffix)
// 判断当前字符串是否以某个子字符串结尾。
System.out.println("test.txt".endsWith(".java")); // false
System.out.println("test.txt".endsWith(".txt")); // true
System.out.println("fdsajklfhdkjlsahfjkdsahjklfdss".endsWith("ss")); // true
// 10(掌握).byte[] getBytes()
// 将字符串对象转换成字节数组
byte[] bytes = "abcdef".getBytes();
for(int i = 0; i < bytes.length; i++){
System.out.println(bytes[i]);
}
// 11(掌握)、char[] toCharArray()
// 将字符串转换成char数组
char[] chars = "我是中国人".toCharArray();
for(int i = 0; i < chars.length; i++){
System.out.println(chars[i]);
}
// 12(掌握).String[] split(String regex)
// 拆分字符串
String[] ymd = "1980-10-11".split("-"); //"1980-10-11"以"-"分隔符进行拆分。
for(int i = 0; i < ymd.length; i++){
System.out.println(ymd[i]);
}
String param = "name=zhangsan&password=123&age=20";
String[] params = param.split("&");
for(int i = 0; i <params.length; i++){
System.out.println(params[i]);
// 可以继续向下拆分,可以通过“=”拆分。
}
// 13(掌握)、String toLowerCase()
// 转换为小写。
System.out.println("ABCDefKXyz".toLowerCase());
// 14(掌握)、String toUpperCase();
//转换为大写
System.out.println("ABCDefKXyz".toUpperCase());
// 15(掌握). String replace(CharSequence target, CharSequence replacement)
// 替换。将某个内容全部替换成指定内容
// String的父接口就是:CharSequence
String newString = "http://www.baidu.com".replace("http://", "https://");
System.out.println(newString); //https://www.baidu.com
// 把以下字符串中的“=”替换成“:”
String newString2 = "name=zhangsan&password=123&age=20".replace("=", ":");
System.out.println(newString2); //name:zhangsan&password:123&age:20
//16(掌握).String replaceAll(CharSequence target, CharSequence replacement)
// 替换。将某个内容全部替换成指定内容,支持正则
//17(掌握).String replaceFirst(CharSequence target, CharSequence replacement)
// 替换。将某个第一次出现的内容全部替换成指定内容,支持正则
// 18(掌握)、 String substring(int beginIndex) 参数是起始下标。
// 截取字符串:从指定下标截取到字符串的最后
System.out.println("http://www.baidu.com".substring(7)); //www.baidu.com
// String substring(int beginIndex, int endIndex)
// beginIndex起始位置(包括)
// endIndex结束位置(不包括)
System.out.println("http://www.baidu.com".substring(7, 10)); //www
// 19(掌握).boolean isEmpty()
// 判断某个字符串是否为“空字符串”。底层源代码调用的应该是字符串的length()方法。
//String s = "";
String s = "a";
System.out.println(s.isEmpty());
// 20(掌握). String trim();
// 去除字符串前后空白
System.out.println(" hello world ".trim());
// 21(掌握). String中只有一个方法是静态的,不需要new对象
// 这个方法叫做valueOf
// 作用:将“非字符串”转换成“字符串”
//String s1 = String.valueOf(true);
//String s1 = String.valueOf(100);
//String s1 = String.valueOf(3.14);
// 这个静态的valueOf()方法,参数是一个对象的时候,会自动调用该对象的toString()方法吗?
String s1 = String.valueOf(new Customer());
//System.out.println(s1); // 没有重写toString()方法之前是对象内存地址 [email protected]
System.out.println(s1); //我是一个VIP客户!!!!
// 我们是不是可以研究一下println()方法的源代码了?
System.out.println(100);
System.out.println(3.14);
System.out.println(true);
Object obj = new Object();
// 通过源代码可以看出:为什么输出一个引用的时候,会调用toString()方法!!!!
// 本质上System.out.println()这个方法在输出任何数据的时候都是先转换成字符串,再输出。
System.out.println(obj);
System.out.println(new Customer());
}
}
class Customer {
// 重写toString()方法
@Override
public String toString() {
return "我是一个VIP客户!!!!";
}
}边栏推荐
- squid代理服务器
- Day8 ---- 云资讯项目介绍与创建
- mysql使用笔记一
- "Good voice" has been singing for 10 years. How can the Chinese language in the starry sky sing well in HKEx?
- SQL必需掌握的100个重要知识点:检索数据
- 系统自带的karsonzhang/fastadmin-addons报错
- 2021全球独角兽榜发布:中国301家独角兽企业全名单来了!
- 爱数课实验 | 第九期-利用机器学习方法进行健康智能诊断
- Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
- oss上传调用的是哪个方法
猜你喜欢

MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)

送你12个常用函数公式,用过的都说好

100 important knowledge points that SQL must master: using functions to process data

分享|智慧环保-生态文明信息化解决方案(附PDF)

GoLand permanently activated

Zhongang Mining: the largest application field of new energy or fluorite

How to participate in openharmony code contribution

使用storcli工具配置RAID,收藏这一篇就够了

JPA踩坑系列之save方法

MySQL usage notes 1
随机推荐
基于微信小程序的警局报案便民服务平台#毕业设计
After kotlin wechat payment callback, the interface is stuck and uipagefragmentactivity windowleft is thrown
Can Oracle's CTAs bring constraints and other attributes to the new table?
MySQL usage notes 1
Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
Wechat applet based service management system for college party members' Home System applet graduation design, Party members, activists, learning, punch in, forum
Codeforces Round #717 (Div. 2)
让马化腾失望了!Web3.0,毫无希望
MySQL client tools are recommended. I can't imagine that it is best to use Juran
一套系统,减轻人流集中地10倍的通行压力
SQL必需掌握的100个重要知识点:检索数据
开启生态新姿势 | 使用 WrodPress 远程附件存储到 COS
MySQL performance optimization index function, hidden, prefix, hash index usage (2)
如何将队列里面的内容没秒钟执行一次优先级
Icml2022 | scalable depth Gaussian Markov random field
Ceph分布式存储
Model reasoning acceleration based on tensorrt
SQL必需掌握的100个重要知识点:创建计算字段
"Good voice" has been singing for 10 years. How can the Chinese language in the starry sky sing well in HKEx?
覆盖接入2w+交通监测设备,EMQ 为深圳市打造交通全要素数字化新引擎