zoukankan      html  css  js  c++  java
  • java基础-字符串

    1、是什么

        在Java语言中字符串必须包含在一对“ ”(双引号)之内。例如:

        "23.23"、"ABCDE"、"你好"

        这些都是字符串常量,字符串常量是系统能够显示的任何文字信息,甚至是单个字符。

        

    2、创建字符串

        String类即字符串类型,并不是Java的基本数据类型,但可以像基本数据类型一样使用,用双引号括起来进行声明。在Java中用String类的构造方法来创建字符串变量。几种常用的构造方法如下:

        String():一个String对象,使其表示一个空字符序列。 

        String(char a[]):用一个字符数组a创建String对象。

        String(char a[], int offset, int length)

        提取字符数组a中的一部分创建一个字符串对象。参数offset表示开始截取字符串的位置,length表示截取字符串的长度。


    3、连接字符串

        使用“+”运算符可完成对多个字符串连接的功能。“+”运算符可以连接多个运算符并产生一个String对象。

        字符串也可同其他基本数据类型进行连接。如果将字符串同这些数据类型数据进行连接,会将这些数据直接转换成字符串。


    4、获取字符串信息

    (1)使用String类的length()方法可获取声明的字符串对象的长度。

        语法如下:

        str.length();

        其中,str为字符串对象。

    public class StringExample {
        public static void main(String[] args) {
            String s = new String("student");
            System.out.println(s.length());
        }
    }


    (2)String类提供了两种查找字符串的方法,即indexOf()与lastIndexOf()方法。这两种方法都允许在字符串中搜索指定条件的字符或字符串。indexOf()方法返回的是搜索的字符或字符串首次出现的位置,lastIndexOf()方法返回的是搜索的字符或字符串最后一次出现的位置。

        1)indexOf(String s)

        2)lastIndexOf(String str)

    public class StringExample {
        public static void main(String[] args) {
            String s = new String("student");
            System.out.println(s.indexOf("t"));
            System.out.println(s.lastIndexOf("t"));
        }
    }

    (3)使用charAt()方法可将指定索引处的字符返回。

        语法如下:

        str.charAt(int index)

        str:任意字符串。

        index:整型值,用于指定要返回字符的下标。

    public class StringExample {
        public static void main(String[] args) {
            String s = new String("student");
            System.out.println(s.charAt(2));
        }
    }

     

    5、截取字符串

        通过String类的substring()方法可对字符串进行截取。这些方法的共同点就是都利用字符串的下标进行截取,并应明确字符串下标是从0开始的。

        substring()方法被两种不同的方法重载,来满足不同的需要。

        (1)substring(int beginIndex)

        (2)substring(int beginIndex, int endIndex)

    public class StringExample {
        public static void main(String[] args) {
            String s = new String("student");
            System.out.println(s.substring(1));
            System.out.println(s.substring(2,4));
        }
    }


    6、去空格

        trim()方法返回字符串的副本,忽略前导空格和尾部空格。

        语法如下:

        str.trim()

        其中,str为任意的字符串对象。

    public class StringExample {
        public static void main(String[] args) {
            String s = new String(" s tudent ");
            System.out.println(s.trim());
        }
    }

    7、字符串替换

        replace()方法可实现将指定的字符或字符串替换成新的字符或字符串。

        语法如下:

        str.replace(char oldChar,char newChar)

        oldChar:要替换的字符或字符串。

        newChar:用于替换原来字符串的内容。

        replace()方法返回的结果是一个新的字符串。如果字符串oldChar没有出现在该对象表达式中的字符串序列中,则将原字符串返回。

    public class StringExample {
        public static void main(String[] args) {
            String s = new String("hello world");
            s=s.replace("world","beijing");
            System.out.println(s);
        }
    }

    8、判断字符串的开始与结尾

        startsWith()方法与endsWith()方法分别用于判断字符串是否以指定的内容开始或结束。这两个方法的返回值都为boolean类型。

        startsWith()方法

        该方法用于判断当前字符串对象的前缀是否是参数指定的字符串。

        语法如下:str.startsWith(String prefix)

        其中,prefix是指作为前缀的字符。

        endsWith()方法

        该方法用于判断当前字符串是否是以给定的子字符串结束。

        语法如下:str.endsWith(String suffix)

        其中,suffix是指作为后缀的字符串。

        String类的startsWith()方法与endsWith()方法分别用于判断字符串是否以指定的内容开始和结尾。这两个方法的返回值都为boolean类型。

    public class StringExample {
        public static void main(String[] args) {
            String s = new String("hello world");
            s=s.replace("world","beijing");
            System.out.println(s.startsWith("l"));
            System.out.println(s.endsWith("jing"));
        }
    }

    9、判断字符串是否相等

        判断字符串是否相等有equals()、equalsIgnoreCase()两种方法,而又有各自的规则,使用不好就会出错,所以一定要区分好什么时候用哪种方法。

        

        (1、使用equals( )方法比较两个字符串是否相等。它具有如下的一般形式:

        boolean equals(Object str)

        这里str是一个用来与调用字符串(String)对象做比较的字符串(String)对象。如果两个字符串具有相同的字符和长度,它返回true,否则返回false。这种比较是区分大小写的。

        (2、为了执行忽略大小写的比较,可以调用equalsIgnoreCase( )方法。当比较两个字符串时,它会认为A-Z和a-z是一样的。其一般形式如下:

        boolean equalsIgnoreCase(String str)

        这里,str是一个用来与调用字符串(String)对象做比较的字符串(String)对象。如果两个字符串具有相同的字符和长度,它也返回true,否则返回false。

    public class StringExample {
        public static void main(String[] args) {
            String s = new String("hello world");
            String s2 = "Hello World";
            System.out.println(s.equals(s2));
            System.out.println(s.equalsIgnoreCase(s2));
        }
    }

    10、按字典顺序比较两个字符串

        compareTo()方法为按字典顺序比较两个字符串,该比较基于字符串中各个字符的Unicode值,按字典顺序将此String对象表示的字符序列与参数字符串所表示的字符序列进行比较。如果按字典顺序此String对象位于参数字符串之前,则比较结果为一个负整数;如果按字典顺序此String对象位于参数字符串之后,则比较结果为一个正整数;如果这两个字符串相等,则结果为0。

        语法如下:

        str.compareTo(String otherstr)

        其中,str、otherstr是参加比较的两个字符串对象。

    public class StringExample {
        public static void main(String[] args) {
            String s = new String("b");
            String s2 = "a";
            String s3 = "c";
            System.out.println("s compare s2 "+ s.compareTo(s2));
            System.out.println("s compare s3 "+ s.compareTo(s3));
        }
    }


    11、字母大小写转换

        字符串的toLowerCase()方法可将字符串中的所有字符从大写字母改写为小写字母,而toUpperCase()方法可将字符串中的小写字母改写为大写字母。


    12、日期和时间字符串格式化

        在应用程序设计中,经常需要显示时间和日期。如果想输出满意的日期和时间格式,一般需要编写大量的代码经过各种算法才能实现。format()方法通过给定的特殊转换符作为参数来实现对日期和时间的格式化。

        使用format()方法对日期进行格式化时,会用到日期格式化转换符,常用的日期格式化转换符如下所示。

    import java.util.Date;
    
    public class DateExample {
        public static void main(String[] args) {
            Date date = new Date();
            String s = String.format("%te",date);
            System.out.println(date);
            System.out.println(s);
        }
    }

       

        使用format()方法对时间进行格式化时,会用到时间格式化转换符,时间格式化转换符要比日期转换符更多、更精确,它可以将时间格式化为时、分、秒、毫秒。常用的时间格式化转换符如下表所示。

      

    import java.util.Date;
    
    public class Date2 {
        public static void main(String[] args) {
            Date date = new Date();
            String hour = String.format("%tH",date);
            String minute = String.format("%tM",date);
            String second = String.format("%tS",date);
    
            System.out.println("现在时间是:"+hour+":"+minute+":"+second);
        }
    }

        格式化时间常用组合:

    import java.util.Date;
    
    public class Date2 {
        public static void main(String[] args) {
            Date date = new Date();
            String time = String.format("%tc",date);
            String f = String.format("%tF",date);
    
            System.out.println("全年信息:"+time);
            System.out.println("年月日格式::"+f);
        }
    }

        在程序设计过程中,经常需要对常规类型的数据进行格式化,例如格式化为整数,格式化为科学计数表示等,在Java中可以使用常规类型的格式化转换符来实现,下表列出了常规类型的格式化转换符。

    public class General {
        public static void main(String[] args) {
            String s1 = String.format("%d",100/2);
            String s2 = String.format("%b",3>5);
            String s3 = String.format("%x",300);
    
            System.out.println("100的一半是:"+s1);
            System.out.println("3>5正确嘛:"+s2);
            System.out.println("300的十六进制是:"+s3);
        }
    }


    13、字符串生成器

        StringBuilder类,即字符串生成器,新创建的StringBuilder对象初始容量是16个字符,可以自行指定初始长度,也可以动态地执行添加、删除和插入等字符串的编辑操作,大大提高了频繁增加字符串的效率。如果附加的字符超过可容纳的长度,则StringBuilder对象将自动增加长度以容纳被附加的字符。

        StringBuilder类的构造方法有很多,主要是参数上的区别,这里我们主要介绍几种在编程中经常会用到的方法。

        StringBuilder append(String str)方法:追加

        StringBuilder append(StringBuffer sb)方法:

        StringBuilder insert(int offset, String str) 方法:指定位置插入

        StringBuilder delete(int start, String end)方法:指定位置删除

        String toString() 方法

        优势:提高频繁追加字符串时的效率。

    public class StringBulder {
    
        public static void main(String[] args) {
            StringBuilder b = new StringBuilder("");
            for(int i = 0 ; i < 10 ; i++){
                b.append(" a"+i);
            }
            System.out.println(b);
            b.insert(3,"-b-");
            System.out.println(b);
            b.delete(5,180);
            System.out.println(b);
        }
    }

    14、判断是否符合正则表达式的方法

        为了检查输入的数据是否满足某种格式,从JDK1.4开始可以使用String类的matches()方法进行判断。语法如下所示:

        boolean matches(String regex) 

        regex:指定的正则表达式

        返回值:返回boolean类型

        该方法用于告知当前字符串是否匹配参数regex指定的正则表达式。返回值是boolean类型,如果当前字符串与正则表达式匹配,则该方法返回true,否则返回false。


    15、正则表达式的元字符

        正则表达式是由一些含有特殊意义的字符组成的字符串,这些含有特殊意义的字符称为元字符,下表列出了正则表达式的部分元字符。


    16、正则表达式的限定符

        在使用正则表达式时,如果需要某一类型的元字符多次输出,逐个输入就相当麻烦,这时可以使用正则表达式的限定元字符来重复次数。下表列出了常用限定符及其含义。

    17、方括号中元字符的含义

        在正则表达式中还可以用方括号把多个字符括起来,方括号中各种正则表达式代表不同的含义。下表列出了方括号中元字符及其含义。



  • 相关阅读:
    0001 工作业务问题_滞纳金公式计算区别实例
    001 win10下安装linux子系统--Ubuntu及其图形界面
    Java知识系统回顾整理01基础07类和对象01引用
    Java知识系统回顾整理01基础06数组07数组工具类Arrays
    Java知识系统回顾整理01基础06数组06二维数组
    Vue 框架中添加百度地图组件
    VUE中使用vue.nextTick 报this.nextTick is not a function错误
    VUE 饿了么项目实战 VUE &quot;TypeError: Cannot read property 'deliveryPrice' of undefined&quot;报错
    VUE vue2.0配置webpack.dev.conf.js加载本地json数据
    VUE response.json() 的更新问题
  • 原文地址:https://www.cnblogs.com/pangchunlei/p/12552657.html
Copyright © 2011-2022 走看看