zoukankan      html  css  js  c++  java
  • 第九天学习

    字符串操作实例

    字符串剪切

    删除一个字符串中的指定字符

    String str = "Hello world";
    str = str.subString(0,4) + str.subString(4);// str = "hell world"
    

    说明:substring()带两个参数时返回对应索引范围内字符串,返回一个参数时返回从数字开始到字符串末尾的字符,类似python的[4:-1];

    字符串替换

    str.replace("H","h");// 把字符串的H替换为h
    

    字符串反转

    StringBuffer str = new StringBuffrt("abcd");
    str = str.reverse();// str = "dcba"
    

    注意只有字符对象才能使用reverse()方法

    字符串搜索

    int n = str.indexOf("orld");
    

    说明:搜索字符串是否包含目标字符串,如果有返回索引,如果不存在返回-1

    字符串分割

    String str = "www.baidu.com";
    String[] temp = str.split("\.");// 按点分割为三个单词,"."号需要转义
    

    获取当前时间

    long time = System.currentTimeMillis();//返回时间戳,注意用long类型声明
    

    字符串拼接

    使用"+"比使用StringBuffer.append()快很多

    数组

    数组排序及搜索

    import java.util.Array;
    Arrays.sort(array);// 排序
    int index = Arrays.binarySearch(array, 'a');// 搜索array数组里面有没有字符'a',返回索引,如果没有返回-1或-5
    

    注意:集合转换为数组才能使用Arrays的方法,对象转数组的方法:object.toArray(),该查找方法是二分法查找,只能对有序数组使用

    数组反转

    import java.util.Collection;
    
    ArrayList<String> alist = new ArrayList<>();
    alist.add("a");
    alist.add("b");// 数组对象可以用add方法插入数据
    alist.add("c");
    Collection.reverse(alist);
    

    注意使用集合Collection操作列表

    数组插入和删除元素

    ArrayList<String> list = new ArrayList<>();
    list.add('a');// 
    list.remove('a');
    

    注意:只有集合才能使用此方法

    数组和集合之间的相互转换

    List -> Array

    List<String> list = new ArrayList<String>();
    String[] array =new String[];
    array = list.toArray();
    

    Array ->List

    List<String> list = new ArrayList<String>();
    String[] array =new String[];
    list = array.asList();
    

    注意:由数组转换的集合无法操作,不能用add和remove方法

    数组中查找指定函数

    ArrayList<String> list = new ArrayList<String>();
    list.contains("a");
    

    注意使用集合才能判断

  • 相关阅读:
    lostash 正则
    Mysql 利用multiline 实现多行匹配
    java中byte, int的转换
    mysql perl 抓取update语句
    $/ 改变换行符
    mysql 匹配update
    perl binlog dml操作报告
    mysql 分区 按 PARTITION BY RANGE (TO_DAYS(startTime))
    Mysql explain 查看分区表
    写作的力道——北漂18年(番外篇一)
  • 原文地址:https://www.cnblogs.com/zhz-8919/p/10653485.html
Copyright © 2011-2022 走看看