zoukankan      html  css  js  c++  java
  • java String方法

    String():初始化一个新的 String 对象,使其表示一个空字符序列,并不是返回空(不等于 null)。

    String(StringBuffer buffer):根据StringBuffer对象来创建String对象;

    String(StringBuilder builder):同上

    char charAt(int index):取字符串中指定位置的字符,index从0开始计算。

    String concat(String str):连接字符串,等同于 “+”;

    boolean contentEquals(StringBuffer buffer):若二者包含的字符序列相同时就返回true;

    boolean equals(Object obj):将该字符串与指定对象比较,若二者包含的字符序列相等返回true;

    boolean equalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写;

    byte[] getBytes():将该字符串转换成byte数组;

    int indexOf(String str):找出str字符串在该字符串中第一次出现的位置;

    int indexOf(String str, int fromIndex) 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始;

    int lastIndexOf(String str) 返回指定子字符串在此字符串中最后一次出现处的索引;

    int length():返回当前字符串长度;

    String replace(char oldChar, char newChar) :返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

    String replaceAll(String regex, String replacement) 使用给定的 字符串replacement 替换此字符串所有的regex字符串;

    boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始.

    String[] split(String regex): 把字符串按指定的字符串分隔开。

    String substring(int beginIndex) 返回一个新的字符串,从beginIndex开始截取,它是此字符串的一个子字符串;

    String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串;[begin,end)

    char[] toCharArray() 将此字符串转换为一个新的字符数组。

    String toLowerCase() 将此 String 中的所有字符都转换为小写;

    String toUpperCase()转成大写;

    static String valueOf(基本类型 obj):把基本类型值转成字符串;

    String trim() :忽略字符串前导空白和尾部空白。

    String小练习

    判断字符串是否由数字组成:

    package reviewDemo;

    public class Demo20 {

        public static void main(String[] args) {

            String s1 = "123456789";

            String s2 = "12345  6789";

            System.out.print(isnum(s1));

    System.out.print(isnum(s2));

        }

       

        public static boolean isnum(String s){

            char []ch = s.toCharArray();

            for (char c : ch) {

                if(!(c > '0' && c <= '9')){

                    return false;

                }

            }

            return true;

        }

    }

    输出:

    true

    false

    判断字符串是否为空:

    分析:

    字符串的空有两种情况:1、null;2、"";

    package reviewDemo;

    public class Demo21 {

        public static void main(String[] args) {

           

            String s1 = "";

            System.out.println(isimpty(s1));

           

        }

       

        public static String isimpty(String s){

            if(s != null & !"".equals(s)){

                return "不为空";

            }

            return "为空!";

        }

    }

  • 相关阅读:
    新能源汽车三大核心技术
    新能源汽车分类解读
    hadoop(四):配置参数
    HDP2.4安装(六):小结与回顾
    【Web】Nginx配置开机启动
    【Linux】ODBC安装
    【Erlang】源码安装
    【Linux】 Ncures库的介绍与安装
    【RabbitMQ】 RabbitMQ安装
    《MinDoc 接口文档在线管理系统》
  • 原文地址:https://www.cnblogs.com/fanweisheng/p/11132218.html
Copyright © 2011-2022 走看看