charAt
获取指定位置的字符!
package String;
/**
* 获取指定位置的字符
* @author WS
*
*/
public class CharAtDemo {
public static void main(String[] args) {
//获取当前字符串第十个字符
String str ="thinking in java";
char at = str.charAt(9);
System.out.println(at);
}
}
indexOf
检索指定字符串当前字符串的位置,没有找到的话会返回-1!其中有几个重载的方法,详细请看代码:
package String;
/**
* int indexOf(tring str)
* 检索指定字符串在当前字符串的位置,
* 如果没找到则会返回-1
*
* @author WS
*
*/
public class IndexOfDemo {
public static void main(String[] args) {
String str = "thinking in java ";//java 编程思想
//第一次出现的位置
int index = str.indexOf("in");
System.out.println(index);//2
//指定位置往后找
index = str.indexOf("in",4);
System.out.println(index);//5
//最后一次出现的位置
index = str.lastIndexOf("in");
System.out.println(index);
index = str.indexOf("IN");
System.out.println(index);//-1
}
}
length
返回当前字符串的长度,也就是字符串含有字符的个数!
package String;
/**
* int length()
* 该方法可以返回当前字符串的长度(字符串个数)
* @author WS
*
*/
public class LengthDemo {
public static void main(String[] args) {
String str = "woaiJava!";
int length = str.length();
System.out.println(length);
}
}
replaceAll
将当前字符串中满足正则表达式的部分替换为给定的内容,并将替换后的字符串返回!
package String;
public class ReplaceAllDemo {
/*
* String replaceAll(String regex ,String str)
* 将当前字符串中满足正则表达式的部分替换为给定的内容
* 并将替换后的字符串返回
*
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str ="abc123def456ghi";
/*
* 将当前字符串的数字部分替换成#mumber#
*/
str=str.replaceAll("[0-9]+", "#NUMBER#");
System.out.println(str);
/*
* 和谐语言
*/
String regex ="wqnmlgb|dsb|cnm|nc|dsd";
String message = "wqnmlgb!你个dsd,你怎么那么nc!!!";
message=message.replaceAll(regex, "***");
System.out.println(message);
}
}
startsWith
endWith
判断当前字符串是不是给定的字符串结尾或者开头的!
package String;
/**
* boolean startsWith(String str)
* boolean endWith(String str)
* 判断当前字符串是不是以给定的字符串结尾的
* @author WS
*
*/
public class SEWithDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "thinking in java";
if(str.endsWith("java")&&str.startsWith("thinking")) {
System.out.println("是以thinking开头, Java结尾的!");
}else {
System.out.println("不是以指定字符串开头或结尾的!");
}
}
}
split
将当前字符串按照满足的正则表达式进行划分,一般使用数组来接收!
package String;
import java.util.Arrays;
/**
* String[] split(String regex)
* 将当前字符串按照满足的正则表达式进行划分
* 通常用一个数组来接收
* @author WS
*
*/
public class SplitDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abc123def456ghi";
String [] arr = str.split("[0-9]+");
System.out.println(arr.length);
System.out.println(Arrays.toString(arr));
//拆分一些没有特殊其它意义的字符时只需要“该字符”就可以
//拆分“.”时则需要“\\.”
/*
* str=",aaa,bbb,ccc";
* str="aaa,,bbb,ccc"
* 如果字符串一开始就匹配到拆分时
* 会拆分出一个空字符串;
* 如果两个拆分字符连续在一起中间也会拆分出一个空字符;
* 他的长度是实际存在的
*/
str="aaa,bbb,ccc,,,,,,";
/*
* 如果拆分项的匹配在最后
* 则会自动删除末尾的空串
* 该长度就只有3
*/
}
}
substring
截取指定范围的字符串并不会改变被截取的字符串!通常在API中表示的范围都是“含头不含尾”的!
package String;
/**
* String substring(int start,int end)
* 截取指定范围的字符串(不改变字符串本身)
* start开始的位置end结束位置(截取时不包含结束位置)
* 注:
* 通常在API中表示范围时都是“含头不含尾”的!!!!!
* @author WS
*
*/
public class SubstringDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "www.tedn.cn";
//截取域名
String sub = str.substring(4, 8);
System.out.println(sub);
//指定位置,取往后的所有
sub= str.substring(4);
System.out.println(sub);
}
}
toUpperCase
toLowerCase
将字符串英文部分替换全大写或者小写!
package String;
/*
* String toUpperCase()
* String toLowerCase()
* 将字符串英文部分转换为大写或者小写
*
*/
public class ToUpLoCase {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "我爱Java";
String up = str.toUpperCase();
String lo = str.toLowerCase();
System.out.println(up);
System.out.println(lo);
}
}
trim
去掉字符串两边空白字符它也是不会改变被去掉空白的字符串,也就是说原来的字符串还是会有空白,需要使用的是他返回的那个字符串吗,那个才是去除了空白的!空白不等同于空格,空白的范围更加大,TAP也属于其中!
package String;
/**
* String trim()
* 去除字符串两边的空白字符(不改变字符串本身 )
* 空白不等同于空格;空白含义更广TAP也属于
* @author WS
*
*/
public class TrimDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = " hello ";
String trim = str.trim();
System.out.println(trim);
}
}
ValueOfDemo
将XXX转化为字符串!!!
package String;
/**
* static String valueOf(XXX xxx)
* String 提供一套重载的valueOf方法,
* 他们是静态方法,
* 将给定的类型转化为String
* @author WS
*
*/
public class ValueOfDemo {
public static void main(String[] args) {
// 方法1
int a =123;
String s1 = String .valueOf(a);
System.out.println(s1);
//方法2 基本类型和字符串连接都会变成字符串类型
String s2 = a+" ";
System.out.println(s2);
}
}
程序员初成长路线:(很全面的学习视频,网址…点击查看)https://blog.csdn.net/qq_43541242/article/details/107165068