前言
好久不见啦,我们要继续了哦
这一次,我们将一口气,将所有String类的常用方法全部记下来,包括方法名称、参数作用以及类型,因此本文可以当作Java String类方法的字典,有需要可以看本文查方法。
一个成熟的编程语言,除了它的语法非常完善之外,也需要提供大量的开发类库,开发类库除了本身提供的JDK Doc之外还会有大量的别的开发类库。
首先我们打开JDK Doc看一下,下面是我上传的免费资源,包含doc可以直接下载
https://download.csdn.net/download/weixin_49856935/89008450
点开doc文件夹后,找到index.html即可查看java开发类库文档,如果要想找到String类的说明,则需要从左上角找java.lang,然后在左下角找到String即可查看,如下图所示。
开干!!!!!!!!!!!!!!
一、字符与字符串
Java提供一系列和字符串相关的方法。
1.构造方法
public String(char[] value)
将全部的字符数组作为String的内容
public String(char[] value,int offset,int count)
将部分的字符数组变为字符串,设置字符数组的开始索引(offset)和使用个数(count)
2.普通方法
public char charAt(int index)
返回指定索引位置上的字符
public char[] toCharArray()
将字符串以字符数组形式返回
来个例子,上代码:
public class javatest1 {
public static void main(String args[]){
String str="hello";
char c=str.charAt(0);
System.out.println(c);
}
}
public class javatest1 {
public static void main(String args[]){
String str="hello";
char[] data=str.toCharArray();
System.out.println(data[0]);
}
}
于是都会输出h。
于是这个时候就可以做一个基本的算法题啦:将字符串中的小写字母变成大写字母
代码如下:
public class javatest1 {
public static void main(String args[]){
String str="hello";
char[] data=str.toCharArray();//将字符串变为字符数组
for(int x=0;x<data.length;x++){
data[x]-=32;
}
System.out.println(new String(data));
System.out.println(new String(data,2,2));//构造方法
}
}
于是就会输出HELLO,注意,最后的输出用了上面的两种构造方法(是不是很综合哈哈哈)。
二、字节与字符串
1.构造方法
public String(byte[] bytes)
将全部字节数组变为字符串
public String(byte[] bytes,int offset,int length)
将部分字节数组变为字符串
2.普通方法
public byte[] getBytes()
将字符串变为字节数组
public byte[] getBytes(String charsetName)throws UnsupportedEncodingException
编码转化
我们来试一下,用字节数组实现小写字母变为大写字母的操作。
public class javatest1 {
public static void main(String args[]){
String str="hello";
byte data[]=str.getBytes();//将字符串变为字节数组
for(int x=0;x<data.length;x++){
data[x]-=32;//改变编码
}
System.out.println(new String(data));
}
}
但还是建议各位先使用字符数组来完成如上这样的需求。
三、字符串
1.字符串比较
我们之前讲过,可以用equals来比较字符串,但方法是属于区分大小写的比较,为此在Java里面,我们针对于字符串的比较实际上还有其他方法。
普通方法
public boolean equals(String anObject)
区分大小写的字符串比较
public boolean equalsIgnoreCase(String anotherString)
不区分大小写的字符串比较
public int compareTo(String anotherString)
比较字符串大小
public int compareToIgnoreCase(String str)
不区分大小写比较字符串大小
我们来看看代码:
public class javatest1 {
public static void main(String args[]){
String strA="hello";
String strB="HELLO";
System.out.println(strA.equals(strB));//false
System.out.println(strA.equalsIgnoreCase(strB));//true
}
}
纳尼?为什么要这么比较呀??什么比较要求不区分大小写呀??
答案是:验证码 0.0
各位会发现验证码不区分大小写
而在进行字符串相等的比较操作之中,最为关键的一个方法是compareTo(),这个方法本身返回int型数组,而这个int型数据有三种结果:如果大于,返回结果大于0,如果小于,返回结果小于0,如果等于,返回结果等于0;
来个例子:a和A谁大??猜猜看
上代码:
public class javatest1 {
public static void main(String args[]){
String strA="A";
String strB="a";
System.out.println(strA.compareTo(strB));
}
}
代码输出了-32,意味着小于,意味着A<a,
在compareTo()方法之中,要进行比较是依据编码进行相减得来的。
各位可以试一下使用compareToIgnoreCase()输出的结果是什么,并解释一下原因,也就是运行如下代码:
public class javatest1 {
public static void main(String args[]){
String strA="A";
String strB="a";
System.out.println(strA.compareToIgnoreCase(strB));
}
}
2.字符串查找
从一个完整的字符串之中查找一些子字符串,而可以实现这种字符串查找功能的方法有如下几个:
普通方法
public boolean contains(String s)
判断某一个字符串是否存在
public int indexOf(String str)
取得某一个子字符串的索引位置,找不到返回-1
public int indexOf(String str,int fromIndex)
从指定索引位置开始检索子字符串位置,找不到就返回-1
public int lastIndexOf(String str)
从后向前朝朝指定子字符串的位置,找不到返回-1
public int lastIndexOf(String str,int fromIndex)
从指定位置由后向前查找指定位置,找不到返回-1
public boolean startsWith(String prefix)
判断是否以某个字符串开头
public boolean startsWith(String prefix,int toffset)
从指定位置判断是否以某个字符串开头
public boolean endsWith(String suffix)
是否以某个字符串结尾
如果要查找中间内容,现在基本使用contains()了
上代码:
public class javatest1 {
public static void main(String args[]){
String str="hello";
if(str.contains("l")){
System.out.println("已找到");
}
}
}
contains()方法虽然现在用的多,但是其是在JDK1.5后才提供的。而在这之前,只能够通过indexOf()方法。
public class javatest1 {
public static void main(String args[]){
String str="hello";
System.out.println(str.indexOf("l"));
}
}
输出的2指的是第一个满足查找的位置的索引,至于其他的方法,根据我上面你列举出的方法结构,大同小异。
总结
本文讲了好多了哇,希望各位可以自己简单总结一下,关于字符串截取、替换、拆分及其他操作方法,各位可以查看手册啦(我都教给各位怎么查和带着各位学习了一些啦),我们接下来开始进入this关键字的知识点,String拜拜~,如过各位想听截取等方法可以私戳我,我单独再出一篇。