zoukankan      html  css  js  c++  java
  • Java基础学习篇---------String、集合的学习

    一、String常用的方法:

    1. == 实质比较两个对象的地址数值

    String a = "hello"  (hello为匿名对象)

    String a1 = "hello"

    String b = new String("hello")

    String b1 = new String("hello").intern   

    String c = b;

    a == b ----------> false     false的原因:new会为b新开辟一块内存,所以比较时候两个地址不同(String里面含有intern方法)

                                  可以手动向字符串池中添加,运行结果就是ture)

    a == a1 --------> ture       ture的原因:第一次字符串常量会放入“字符串池”中,下次再次申请时候,会指向同一块的内存

                                       如果不存在的话,会自动加入内存池中

    b == c --------->true

    注意:开发时候大部分使用String a1 = "hello"此类方法,避免new String对象会开辟新的内存,造成内存的的浪费。

                                 即使new出新的对象,使用intern()方法也会把字符串加入字符串池中

    2.equals : 字符串比较里面的内容

     b.equals(a)

     hello.equals(a)

    如果String b = null 时候,系统会抛出nullPointExction(空指针异常),开发时候大部分都使用hello.equals(a)此类方法,

    原因:就是可以避免出现字符串为空时候抛出的异常

    3.开发过程中尽量不要大规模使用String + "hello" + "world"............  , 因为这样也会造成内存的浪费

    4.String常用的函数名

     (1). char charAt(int index)  : 查找制定索引的字,第几个位置是什么字母就输出什么字母

     (2). boolean equals(String s)  : 比较两个字符的内容是否相同

     (3). boolean  equalsIgnoreCase(String s) : 比较两个字符串是否相同,忽略大小写

     (4). int compareTo(String s)  : 比较两个字符串的大小关系

     (5). boolean contains(String s) : 查找字符串是否存在

     (6). int indexOf(String s)   : 从起始位置开始查找字符串,存在返回索引数值,不存在返回-1

     (7). int indexOf(String s , int fromIndex )   :  从fromIndex位置开始查找,存在返回索引数值,不存在返回-1

     (8). boolean startWith(String s)   :  判断是否以指定字符串作为起始位置

     (9). boolean startWith(String s , int fromIndex)  :  判断是否已指定字符串从fromIndex位置作为起始位置

     (10). endWith(String s)  :  判断是否已指定字符作为结束位置

     (11). String replaceAll(String s , String replace)  :  把字符串s全部替换成replace字符串

     (12). String replaceFrist(String s , String replace) : 字符串replace只替换第一个s字符串,其余的s字符串的都不用替换

       (13). String[]  split(String s)  :  把字符串s全部拆分成字符串数组          String  data[] = String.split("  ") : 这个是按空格来拆分的 

     (14). String  split(String s , int limit)  : 将字符串s部分拆分成字符串数组个数limit的长度

     (15). String  substring(int beginIndex)  :  从beginIndex位置截取到字符串结尾

     (16). String  substring(int beginIndex , int endIndex)  : 从beginIndex位置截取到字符串endIndex结束

     (17). String intern()  :  把字符串对象加入字符串池中

     (18). boolean isEmpty()  :  判断字符串是否为空

     (19). int length()  :   获得字符串的长度

     (20). String trim()  :  去掉字符串的左右空格

     (21). String  concat(String s)  : 字符串连接

     (22). String  toUpperCase()  :  将字符串小字母转化成大写字母

     (23). String  toLowerCase()  : 将字符串大写字母转成小写字母

    5.字符串转为基本数据

      public static int  parseInt(String s)

      public static double  parseInt(String s)

      public static boolean  parseInt(String s)

    6.基本数据类型转换成字符串

      public static String  valueOf(int 、double、boolean)

    二、集合学习

      

      

      

  • 相关阅读:
    appium 启动失败解决方案
    appium for windows 环境搭建
    HttpClient 4 使用方法的几个例子(代理,StringEntity字符串数据,文件上传)(转载)
    下拉框和单选框复选框的选中的值
    js刷新父页面的方法
    克隆虚拟机ip修改后没改变的原因
    二进制转换与此平台,VMware Workstation不,Workstation 不可恢复,此虚拟环境中的长模式
    在虚拟机(VMware)中安装Linux CentOS 6.4系统(图解) 转
    sql语句常用的函数总结
    java.lang.OutOfMemoryError: Java heap space 。java heap space明确的指出了异常发生的区域,
  • 原文地址:https://www.cnblogs.com/liunx1109/p/9775588.html
Copyright © 2011-2022 走看看