字符串的一些操作方法
字符串
1.字符串比较
1.1 区分大小写比较
public boolean equals(Object anObject);
1.2 不区分大小写比较
public boolean equalsIgnoreCase(String, anotherString);
1.3 比较两个字符串的大小关系
public int compareTo(String ,anotherString);
compareTo( )方法的返回值是一个int型,该数据会根据大小关系返回三类内容
- 返回 >0 当前字符串>目标字符串
- 返回<0 当前字符串<目标字符串
- 返回=0 当前字符串=目标字符串
代码示例
package Test;
//字符串比较大小
public class Test1 {
public static void main(String[] args) {
String str="hello";
String str1="Hello";
System.out.println(str.equals(str1));//区分大小写 false
System.out.println(str.equalsIgnoreCase(str1));//不区分大小写 ture
System.out.println("A".compareTo("a"));//-32
System.out.println("a".compareTo("A"));//32
System.out.println("A".compareTo("A"));//0
System.out.println("AB".compareTo("AC"));//-1
System.out.println("赵".compareTo("钱"));//-1852 根据unicode编码值比较
}
}
2.字符串查找
从一个完整的字符串中判断指定的内容是否存在
2.1 判断一个子字符串是否存在
public boolean contains ( String str);
2.2 从头开始查找子字符串的位置,若找到返回开始索引,查不到返回-1
public int indexof ( String str );
//重载
//从指定位置开始从前往后查找
public int indexof(String str,int fromIndex);//formIndex 指定位置索引
2.3 从后向前查找指定字符串,若找到返回开始索引,否则返回-1;
public int lastIndexOf (String str)
//重载
//从指定位置从后向前查找
public int lastIndeof(String str,int fromIndex);//formIndex
代码示例
public class Test2 {
public static void main(String[] args) {
String str="hello";
System.out.println(str.contains("e"));//true
System.out.println(str.contains("w"));//false
System.out.println(str.indexOf("l"));//2
System.out.println(str.indexOf("w"));//-1
System.out.println(str.indexOf("o",2));//4
System.out.println(str.lastIndexOf("h"));//0
System.out.println(str.lastIndexOf("w"));//-1
System.out.println(str.lastIndexOf("e",4));//1
}
}
2.4 判断是否以指定字符串开头
public boolean startsWith(String str);
//重载
//从指定位置开始判断是否以指定字符串开头
public boolean startseWith(String str,int toffset);
代码示例
package Test;
public class Test2 {
public static void main(String[] args) {
String str ="helloworld";
System.out.println(str.startsWith("he"));//true
System.out.println(str.startsWith("kw"));//false
System.out.println(str.startsWith("lo",3));//从指定位置开始查找//true
System.out.println(str.endsWith("ld"));//true
System.out.println(str.endsWith("ft"));//false
}
}
3.字符串替换
3.1 替换所有的指定内容
public String repalceAll(String regex,String repalcement); //将regex内容全替换为replacement
3.2 替换首个内容
public String replaceFirst(String regex,String replacement)//将regex内容首个替换为replacement
代码示例
package Test;
public class Test3 {
public static void main(String[] args) {
String str="HelloWorld";
System.out.println(str.replaceAll("l","*"));
System.out.println(str.replaceFirst("e","*"));
}
}
//He**oWor*d
//H*lloWorld
4.字符串的拆分
可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串
4.1将字符串全部拆分
public String[] split(String regex);
//代码示例
public class Test4 {
public static void main(String[] args) {
String str="Hello world !!!";
String[] result =str.split(" ");//按照空格拆分
for(String s : result){
System.out.println(s);
}
}
}
/*
Hello
world
!!!
*/
4.2 将字符串按照指定格式拆分为limit个子字符串
public String[] split(String regex,int limit);//返回值为数组
代码示例
public class Test4 {
public static void main(String[] args) {
String str = "Hello world !!!";
String[] result = str.split(" ",2);//按空格拆分 最多拆成两个字符串
//当limit<最大 按照limit拆分 如果大于>最大 按最大处理
for (String s : result) {
System.out.println(s);
}
}
}
4.3 拆分IP地址
public class Test4{
public static void main(String[] args){
String str="192.168.1.1";
String[] result = str.split("\\.");
for(string r:result){
System.out.println(r);
}
}
}
若拆分结果为空,指定字符串需要转移("\")转义;这里的\是双\
4.4多次拆分
//多次拆分
String str3="yuisama:27|yui:25";
String[] result4 = str3.split("\\|");//先用|拆分 拆完放入result4中
for(int i=0;i<result4.length;i++){
String[] temp = result4[i].split(":");//用:拆result4 拆完放入temp中
System.out.println(temp[0]+"="+temp[1]);
}
分析如下
5.字符串截取
从一个完整的字符串之中截取部分内容
5.1 从指定索引截取到结尾
public String substring(int beginIndex);
5.2 截取部分内容(指定截取起始和终点)(左闭右开)
public String substring(int beginIndex,int endIndex);//beginIndex 开始节点 endIndex 结束节点
代码示例
public class Test5 {
public static void main(String[] args){
String str ="helloworld";
System.out.println(str.substring(0));
System.out.println(str.substring(3));
System.out.println(str.substring(3,5));//不包括5 左闭右开
}
}
6. 字符串的其他操作
6.1 去掉字符串左右的空格,保留中间空格
public String trim();
代码示例
// 去掉字符串中的左右空格,保留中间空格
String str = " hello world ";
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");
//[ hello world ]
//[hello world]
6.2 字符串大小写转换
6.2.1字符串转大写
public String toUpperCase();
6.2.2 字符串转小写
public String toLowerCasr();
代码示例
//字符串转大写
String str2 = "hello";
String str3 = "HELLO";
String str4 = "[email protected]#[email protected]$";
System.out.println(str2.toUpperCase());//HELLO
System.out.println(str3.toLowerCase());//hello
System.out.println(str4.toUpperCase());//[email protected]#[email protected]$
6.2.3 字符串连接
public String concat(String str);//等同于 “+”,不入池
代码示例
String str5 = "hello";
System.out.println(str5.concat("world"));
7.将一个字符串的首字母转为大写
public class Test{
public static void main(String[] args){
String str = "";
String str2 = "hello";
}
public static String UpFist(String str){
if("".equals(str)||str==null){
System.out.println("这个字符串是空的!");
return str;
}
if(str.length()>1){
return str.substring(0,1).toUpperCase+str.subString(1);
}
return str.toUpperCase();
}
}