package com.hp.zuoye;
/*
* 过滤字符串中的敏感词汇
* @param content 文本
* @param sensitiveWord 敏感词汇
* @return
*/
public class Demo1 {
public String filterSensitiveWords(String content, String sensitiveWord) {
if (content == null || sensitiveWord == null) {
return content;
}
//获取和敏感词汇相同数量的星号
String starChar = getStarChar(sensitiveWord.length());
//替换敏感词汇
return content.replace(sensitiveWord, starChar);
}
//大部分敏感词汇在10个以内,直接返回缓存的字符串
public static String[] starArr={"*","**","***","****","*****","******","*******","********","*********","**********"};
/**
* 生成n个星号的字符串
* @param length
* @return
*/
private static String getStarChar(int length) {
if (length <= 0) {
return "";
}
//大部分敏感词汇在10个以内,直接返回缓存的字符串
if (length <= 10) {
return starArr[length - 1];
}
//生成n个星号的字符串
char[] arr = new char[length];
for (int i = 0; i < length; i++) {
arr[i] = '*';
}
return new String(arr);
}
}
运行结果
public class Test1 {
public static void main(String[] args) {
Demo1 d1 = new Demo1();
String a = d1.filterSensitiveWords("打劫!我有枪", "枪");
System.out.println("a="+a);
}
}
a=打劫!我有*
Process finished with exit code 0
字符串“北京欢迎你”,替换字符为“郑州欢迎你们”,并输出打印
public class Demo2 {
public static void main(String[] args) {
String s = "北京欢迎你";
String s1 = s.replaceAll("北京欢迎你", "郑州欢迎你们");
System.out.println("s1 = "+s1);
}
}
运行结果
s1 = 郑州欢迎你们
Process finished with exit code 0
字符串“面向对象是以对象为核心..编程思想”,获取并第一个下标和最后一个下标的字符,并输出打印
public class Demo3 {
public static void main(String[] args) {
String s= "面向对象是以对象为核心..编程思想";
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < s.length(); i++) {
System.out.println(sb.charAt(0));
System.out.println(sb.charAt(s.length()-1));
}
}
}
运行结果
面
想
Process finished with exit code 0
将double类型的数据3.1415926转为字符串
public class Demo4 {
public static void main(String[] args) {
Double d = 3.1415926;
BigDecimal bd = new BigDecimal(d.toString());
System.out.println(bd);
}
}
运行结果
3.1415926
Process finished with exit code 0
判断一个字符串是否为空,如果为空,对其赋值,如果不为空,获取字符的个数并打印第一个字符
/*
*判断一个字符串是否为空,如果为空,对其赋值,如果不为空,获取字符的个数并打印第一个字符
* */
public void sky(String s) {
if (s == null || s == "") {
System.out.println("字符串为空");
} else {
char c = s.charAt(0);
System.out.println("长度为" + s.length() + "第一个字符为" + c);
}
}
}
测试类
public class Test5 {
public static void main(String[] args) {
Demo5 h = new Demo5();
String s = "";
String s1 = "adedgxdfh";
h.sky(s);
h.sky(s1);
}
}
运行结果
字符串为空
长度为9第一个字符为a
Process finished with exit code 0
判断a在字符串abca中的位置,如果第一次出现的位置和最后一次出现的位置相同,则替换为*
/*
*判断a在字符串abca中的位置,如果第一次出现的位置和最后一次出现的位置相同,则替换为*
* */
public class Demo6 {
public static void main(String[] args) {
String a = "abca";
if (a.lastIndexOf('a')==a.indexOf("a")){
System.out.println("没有相同");
}
System.out.println("原来a为"+a);
int b = a.lastIndexOf('a');
char c = a.charAt(b);
String s = a.replace(c,'*');
System.out.println("修改为"+s);
}
}
运行结果
原来a为abca
修改为*bc*
Process finished with exit code 0
基本数据类型、包装类、字符串String三者之间的相互转换
以int为例:
1.int->包装类
包装类->int
2.int->String
String->int
3.包装类->String
String->包装类
/*
* 基本数据类型、包装类、字符串String三者之间的相互转换
以int为例:
1.int->包装类
包装类->int
2.int->String
String->int
3.包装类->String
String->包装类
* */
public class Demo7 {
public static void main(String[] args) {
int a = 123456;
Integer a1 = a;
int s = a1;
System.out.println(Integer.toString(a));
System.out.println(s);
String s1 = String.valueOf(a);
System.out.println(s1);
int b = Integer.parseInt(s1);
System.out.println(b);
Integer a2 = 123;
String s2 = String.valueOf(a2);
System.out.println(s2);
Integer a3 = Integer.valueOf(s2);
System.out.println(a3);
}
}
运行结果
123456
123456
123456
123456
123
123
Process finished with exit code 0