各位看官,看完的觉得有用又不嫌麻烦的就给个赞或者给个评论呗,嫌麻烦的也请麻烦一下给个赞呗,想踩一脚的,手下留情,码字不易,且看且珍惜,(我的大刀已经饥渴难耐了)
案例一:利用substring方法截取字符串,达到删除字符串中某个字符的目的
/**
*
* @ClassName: DeleteSomeString
* @Description: 利用substring方法截取字符串,达到删除字符串中某个字符的目的
* @author 少天
* @date 2017年12月6日 上午11:09:36
*
*/
public class DeleteSomeString {
public static void main(String[] args) {
String str="abcdefghigklmnopqrstuvwxyz";
String str2=remnveCharAt(str,10);
System.out.println("新的字符串是:"+str2);
}
/**
*
* @Title: remnveCharAt
* @Description: 使用substring()截取字符串,打到删除字符串的目的<br/>
* substring(int beginIndex)返回一个新的字符串,该子字符串从指定索引处的字符开始,直到此字符串末尾。<br/>
* substring(int beginIndex, int endIndex)该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex
* @param str 要截取的字符串
* @param pos 要截取的位置
* @return String 返回类型
* @throws
*/
public static String remnveCharAt(String str,int pos){
return str.substring(0, pos)+str.substring(pos+1);
}
}
案例一运行结果:
新的字符串是:a b c d e f g h i g l m n o p q r s t u v w x y z
注释:第十位是k 这里把k删除了
案例二:验证字符串左后出现的位置
/**
*
* @ClassName: SearchlastString
* @Description: 验证字符串左后出现的位置 <br/>
* int lastIndex = lastIndexOf("要查找的字符串")
* @author 少天
* @date 2017年12月6日 上午10:57:04
*
*/
public class SearchlastString {
public static void main(String[] args) {
String str="hello world ,hello xiao ming ";
int lastIndex=str.lastIndexOf("xiao");
if (lastIndex==-1) {
System.out.println("没有找到字符串");
}else{
System.out.println("字符串最后出现的位置是:"+lastIndex);
}
}
}
案例二运行结果:
字符串最后出现的位置是:19
案例三:查询字符串出现的位置
/**
*
* @ClassName: SearchStringEmp
* @Description: 查询字符串出现的位置
* @author 少天
* @date 2017年12月6日 上午11:37:06
*
*/
public class SearchStringEmp {
public static void main(String[] args) {
String string="abcdefghijklmnokpqrstuvkwxyz";
int indexOf=string.indexOf("k");
int indexOf2=string.indexOf("k", 11);
System.out.println("K第一次的位置是:"+indexOf);
System.out.println("K从11位开始向后第一位的位置是:"+indexOf2);
}
}
案例三运行结果:
K第一次的位置是:10
K从11位开始向后第一位的位置是:15
案例四:分割字符串 split方法
/**
*
* @ClassName: SplitString
* @Description: 分割字符串 split方法
* @author 少天
* @date 2017年12月6日 下午12:18:47
*
*/
public class SplitString {
public static void main(String[] args) {
String str="www-baidu-com";
String[] temp;
String delimeter="-";//指定分割字符
temp=str.split(delimeter);//分割字符串
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
System.out.println("");
}
//使用foreach输出,使用 " . "点号作为分隔符
String str2="www.baidu.com";
String[] temp2;
String delimeter2="\\.";//使用点 做分隔符需要 转义
temp2=str2.split(delimeter2);//分割字符串
for (String x : temp2) {
System.out.println(x);
System.out.println("");
}
}
}
案例四运行结果:
www
baidu
com
www
baidu
com
案例五:字符串比较, compareTo验证大小写,compareToIgnoreCase 忽略大小写
/**
*
* @ClassName: SpringCompareEmp
* @Description: 字符串比较, compareTo验证大小写,compareToIgnoreCase 忽略大小写
* @author 少天
* @date 2017年12月6日 上午10:51:41
*
*/
public class SpringCompareEmp {
public static void main(String[] args) {
String str="Hello World";
String str2="hello world";
System.out.println(str.compareTo(str2));
System.out.println(str.compareToIgnoreCase(str2));
}
}
案例五运行结果:
-32
0
案例六: 测试两个字符串区域是否相等 regionMatches方法
/**
*
* @ClassName: StringRegionMatch
* @Description: 测试两个字符串区域是否相等 regionMatches方法
* @author 少天
* @date 2017年12月6日 下午1:52:01
*
*/
public class StringRegionMatch {
public static void main(String[] args) {
String str1="hello 678 World";
String str2="hello 67s worlD";
/**
* str1.regionMatches(9, str2, 9, 5)
* 表示将 str1 字符串从第9个字符"W"开始和 str2 字符串的第9个字符"w"开始逐个比较,共比较5对字符,
*/
boolean bo1=str1.regionMatches(9, str2, 9, 5);
boolean bo2=str1.regionMatches(true, 9, str2, 9, 5);
System.out.println("bo1:"+bo1);
System.out.println("bo2:"+bo2);
}
}
案例六运行结果;
bo1:false
bo2:true
案例七:字符串替换 replace(oldString, newString)
/**
*
* @ClassName: StringReplaceEmp
* @Description: 字符串替换 replace(oldString, newString)
* @author 少天
* @date 2017年12月6日 上午11:18:24
*
*/
public class StringReplaceEmp {
public static void main(String[] args) {
String str="hello world";
System.out.println(str.replace("h", "w")+"--->把h替换成w");
System.out.println(str.replace("he", "we")+"--->把he替换成we");
System.out.println(str.replace("he", "ha")+"--->把he替换成ha");
}
}
案例七运行结果
wello world--->把h替换成w
wello world--->把he替换成we
hallo world--->把he替换成ha
/**
*
* @ClassName: StringReverseExample
* @Description: 字符串反转,使用reverse()方法,<br/>
* 思路:<br/>
* 先将String类型的字符转换成StringBuffer类型,在调用reverse()方法+ .toString()
* @author 少天
* @date 2017年12月6日 上午11:26:45
*
*/
public class StringReverseExample {
public static void main(String[] args) {
String str="hello world";
String newstr=new StringBuffer(str).reverse().toString();
System.out.println("旧字符串:"+str);
System.out.println("新字符串:"+newstr);
}
}
案例八运行结果;
旧字符串:hello world
新字符串:dlrow olleh
案例九: 字符串转大写
/**
*
* @ClassName: StringToUpperCaseEmp
* @Description: 字符串转大写
* @author 少天
* @date 2017年12月6日 下午1:42:38
*
*/
public class StringToUpperCaseEmp {
public static void main(String[] args) {
String str= "hello world";
String STR=str.toUpperCase();
String Str=str.toUpperCase(Locale.getDefault());
System.out.println("原始字符串:"+str);
System.out.println("转大写:"+STR);
System.out.println("转大写:"+Str);
}
}
案例九运行结果;
原始字符串:hello world
转大写:HELLO WORLD
转大写:HELLO WORLD