当前位置:网站首页>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客户!!!!";
}
}边栏推荐
猜你喜欢

Very comprehensive dolphin scheduler installation and use documents

Flexible IP network test tool -- x-launch

How to participate in openharmony code contribution

How to do a good job of gateway high availability protection in the big promotion scenario

MySQL performance optimization index function, hidden, prefix, hash index usage (2)

华为伙伴暨开发者大会2022开源时刻全纪录

Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)

AI 绘画极简教程

Model reasoning acceleration based on tensorrt

Zhongang Mining: the largest application field of new energy or fluorite
随机推荐
DO280OpenShift访问控制--security policy和章节实验
Modify large online games through CE modifier
VMware vSphere ESXi 7.0安装教程
Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
squid代理服务器
100 important knowledge points that SQL must master: filtering data
Shuttle hides the return button of the AppBar
Is it safe to open an account and buy stocks? Who knows
100 important knowledge points that SQL must master: using functions to process data
GoLand永久激活
MySQL client tools are recommended. I can't imagine that it is best to use Juran
Save method of JPA stepping pit series
Necessary software tools in embedded software development
MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)
SQL server for circular usage
Graduation design of police report convenience service platform based on wechat applet
释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束
银河麒麟系统局域网文件共享教程
1027 Colors in Mars
Cerebral cortex: predicting children's mathematical skills from task state and resting state brain function connections