字符串知识积累
1.判断某字符串中包含某关键字
方式有2种:如下代码
代码:
package cn.xx.temp.test;
public class includeKeyWord {
public static void main(String[] args) {
String str ="张三,李四,王五";
/**
* 1.方式一
* 字符串 中包含某个关键字 返回 Boolean str.contains("关键字")
*/
if(str!="" && str!=null){
boolean b = str.contains("五");
String s = b?"str字符串中包含\"五\"字 ":"str字符串中没找到\"五\"字 ";//三元表达式
System.out.println("方式一:"+s);
}
if(str!="" && str!=null){
/**
*方式二:
字符串 中包含某个关键字 indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1。
str.indexOf("关键字")
*/
int indexOf = str.indexOf("四");
String s = indexOf>0?"str字符串中包含\"四\"字 ":"str字符串中没找到\"四\"字 ";
System.out.println("方式二:"+s);
}
}
}
2.去除字符串 左右空格
用途:在模糊查询的时候 防止用户误敲空格了
代码:
package xx;
/**
* 去除 字符串 两边的空格
* @date:2017-12-26 10:08
*
*
*/
public class DeleteBlank {
public static void main(String[] args) {
String str=" abc ";
System.out.println(str.length());
System.out.println("----------------------------------");
String trim = str.trim();
System.out.println(trim.length());
//看看去掉空格后的效果
if("abc".equals(trim)){
System.out.println("trim 去掉了 字符串两端的空格!");
}
}
}
3.查询 字符串第一次/最后一次出现的索引位置
代码
package xx;
/**
* 查询 字符串第一次/最后一次出现的索引位置
* @date:2017-12-26 10:08
*
*
*/
public class DeleteBlank {
public static void main(String[] args) {
String str="hello,abcdefghabc";
int i = str.indexOf("abc");
int i2 = str.lastIndexOf("abc");
System.out.println("开始索引位置"+i);
System.out.println("结束索引位置"+i2);
}
}
3.字符串截取
效果:
代码
package xx;
/**
* 字符串的截取
* @date:2017-12-26 10:08
*
*
*/
public class DeleteBlank {
public static void main(String[] args) {
String str="hello,abcdefghabc";
String substr = str.substring(6);
String substr2 = str.substring(6,8);
System.out.println(substr);
System.out.println(substr2);
}
}
4.字符串的替换功能
效果:
代码:
package xx;
/**
* 字符串的替换 功能
* @date:2017-12-26 10:08
*
*
*/
public class DeleteBlank {
public static void main(String[] args) {
String str="hello,abcdefghabc";
String newStr = str.replaceAll("abc", "ABC");//将字符串str中的“abc” 替换为“ABC”
System.out.println(newStr);
}
}
5. 判断字符串是否相等
5.1 equals(String otherstr)
如果两个字符串具有相同的字符和长度,则使用equals()方法比较时,返回true。同时equals()方法比较时区分大小写。
qualsIgnoreCase(String otherstr)
equalsIgnoreCase()方法与equals()类型,不过在比较时忽略了大小写。
验证码(经常用)
package xx;
/**
* 判断字符串是否相等
* @date:2017-12-26 12:43
*
* equals(String otherstr)
如果两个字符串具有相同的字符和长度,则使用equals()方法比较时,返回true。同时equals()方法比较时区分大小写。
qualsIgnoreCase(String otherstr)
equalsIgnoreCase()方法与equals()类型,不过在比较时忽略了大小写。
*
*
*/
public class DeleteBlank {
public static void main(String[] args) {
String str="hello";
System.out.println("hello".equals(str));
System.out.println("HELLO".equalsIgnoreCase(str));//忽略大小写
System.out.println("abc".equalsIgnoreCase(str));
}
}
6.字符串 字母大小写转换
效果:
代码
package xx;
/**
* 判断字符串是否相等
* @date:2017-12-26 12:43
字母大小写转换
str.toLowerCase(); 将大写字母全部转化为小写的
str.toUpperCase(); 将小写字母全部转化为大写的
*/
public class DeleteBlank {
public static void main(String[] args) {
String str="hello";
String str2="WORD";
//要求输出:HELLOword
String changeStr = str.toUpperCase();
//System.out.println(changeStr);
String upperCase = str2.toLowerCase();
//System.out.println(upperCase);
System.out.println(changeStr+upperCase);
}
}
7.字符串分割
效果:
代码
package xx;
/**
* 字符串分割
使用split()方法可以使字符串按指定的分隔字符或字符串对内容进行分割,并将分割后的结果存放在字符数组中。
*/
public class DeleteBlank {
public static void main(String[] args) {
String str="hello_word_ni_hao_shijie";
String[] arr = str.split("_");//以_分割
//循环 拿出数组中的值
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
8.字符串倒叙输出(面试常见)
效果
代码:
package xx;
/**
* 目的:将字符串:abcdef 倒叙输出
*
*/
public class StrFlashBack {
public static void main(String[] args) {
String str="abcdef";//目标字符串
StringBuffer sb = new StringBuffer();//创建一个StringBuffer对象 用于
for (int i = str.length() - 1; i > -1; i--) {//倒叙循环
String temp = String.valueOf(str.charAt(i) );//拿到每一个字符串 并转化为字符串类型
sb.append(temp);//追加到sb对象上
}
System.out.println(sb.toString());//toString 是将其转化为字符串
}
}