zoukankan      html  css  js  c++  java
  • 字符串

    一创建String类型对象的三种方式:

      

    1.String name=new String();
    2.String name=new String("张三");
    3.String name="张三";

    二:==与  .equals()的区别:

      ==:用于比较数据类型,如果用于比较String类型,则比较的是内存首地址;

      .equals():用来比较两个字符串是否相等;

    三:字符串常用方法:

      1.查找字符串的位置:

        搜索在字符串中第一个出现的ch或字符串value

    public int indexOf(int ch);
    public int indexOf(String value);
    
       搜索在字符串中最后一个出现的ch或字符串value
    public int lastIndexOf(int ch);
    public int lastIndexOf(String value);

       2.截取字符串:

        提取从位置开始的字符串部分

    public String substring(int index);

        提取从de到de之间的字符串部分

    public String substring(int be,int be);

        返回一个前后不含任何空格的调用字符串副本

    public String trim();

       3.拼接字符串:

        1:使用“+”号

        2:concat()方法

      4.比较:

        1: .equals();

        2: .equalslgnoreCase()     //忽略大小写

      5.字符串大小写转换

        1:toUpperCase()    //转为大写

        2:toLowerCase()   //转为小写

      6.字符串长度

        1:    .length();

      7.字符串分割制

        

    String str="123,123,123,123";
    //根据什么分割
    String [] split=str.split(",");
    for(int i=0;i<split.length;i++){
        System.out.println(split[i]);
    }

    四:StringBuffer:

        1.StringBuffer:是String的增强版

        2.创建StringBuffer的对象

          StringBuffer sb =new StringBuffer();

          StringBuffer sb =new StringBuffer("啊啊啊");

    五:StringBuffer的使用方法

      (append ();   insert();    toString())

      

    StringBuffer sb=new StringBuffer("超级演说家马上开始");
    //拼接
    sb.appeng("!")
    //在指定位置插入
    sb.insert(5,",");
    //从StringBuffer转为String
    String string=sb.toString();
    System.out.println(string);

    有任何问题可随时联系我QQ:2838509529     原创:梅川酷子

  • 相关阅读:
    Python开发【第六篇】循环语句
    Python开发【第四篇】语句与函数
    Python开发【第三篇】数据类型
    Python开发【第二篇】:初始Python
    2019-10-11入博客第一篇文章
    设计模式——命令模式
    设计模式——职责链模式
    设计模式——单例模式
    设计模式——原型模式
    设计模式——建造者模式
  • 原文地址:https://www.cnblogs.com/whtt/p/9812643.html
Copyright © 2011-2022 走看看