• 1.关于String substring有俩个题 2.String replace(“老值“,“新值“)


     

    1. public class benxi {
    2. public static void main(String[] args) {
    3. //获取手机号
    4. String phone="18538371706";
    5. //用方法substring来获取 包头不包尾
    6. String su = phone.substring(0, 3);
    7. //用方法substring获取 一直到末尾的
    8. String s=phone.substring(7);
    9. //拼接一块
    10. String result=su+"****"+s;
    11. System.out.println(result);
    12. }
    13. }

    1.String substring(int beginIndex,int endIndex)

    注意 这个里面放是索引 包头不包尾 包左不包右

    2.String substring(int beginIndex)

    注意 这个截取到末尾

     

    1. public class benxi {
    2. public static void main(String[] args) {
    3. //定义身份证字符串
    4. String sfz="411523200012260412";
    5. //年份
    6. String n=sfz.substring(6,10);
    7. //月份
    8. String y = sfz.substring(10, 12);
    9. //日
    10. String r = sfz.substring(12, 14);
    11. System.out.println("年份"+n+"月份"+y+"日"+r);
    12. //性别 倒数第二位 单数男双数女
    13. char gender=sfz.charAt(16); //直接输出是字符 ‘3’ 需要变成3 '3'为51
    14. int num=gender-48;
    15. if (num%2==0){
    16. System.out.println("性别为女性");
    17. }else{
    18. System.out.println("性别为男性");
    19. }
    20. }
    21. }

    注意 char gender=sfz.charAt(16); 直接输出是索引16的字符‘3’ 要转换为数字3 ASCII表 可以System.out.println(‘0’+0);查询

    0为48 依次增加所以 gender-48

     

    1. public class benxi {
    2. public static void main(String[] args) {
    3. //敏感词替换
    4. //定义说的话
    5. String talk="你玩的真好 tmd cnm";
    6. //定义 当有很多敏感词时用数组来
    7. String arr[]={"tmd","cnm","mlgb","lj"};
    8. //循环敏感词数组中每一个
    9. for (int i = 0; i < arr.length; i++) {
    10. //词替换然后交回talk
    11. talk=talk.replace(arr[i], "***");
    12. }
    13. System.out.println(talk);
    14. }
    15. }

    String replace("老值","新值")

  • 相关阅读:
    5分钟搞懂布隆过滤器,掌握亿级数据过滤算法
    论文导读 | 并发数据结构与并发控制
    使用mysql的cmd窗口,运行项目中的mapper层xml里的sql语句,查看运行结果
    boost Geometry
    [题]跳房子 #单调队列优化(伪)
    项目实战(一) 瑞吉外卖
    2.可视化基础(上)
    Jenkins实现基础CI操作
    推荐搜索中各类排序算法综述
    线程不安全的原因及其解决方案
  • 原文地址:https://blog.csdn.net/m0_67911983/article/details/128045291