zoukankan      html  css  js  c++  java
  • 字符串之String类

    主要涉及在程序运行初始化之后不能改变的字符串类String和字符串内容可以动态改变的类StringBuffer,以及用于字符串转换词法分析类StringTokenizer;同时还将介绍字符串和其他数据类型之间的相互转换。

    Java中将字符串作为对象来处理,在对象中封装了一系列方法来进行字符串处理。

    String类

    String位于java.lang包中,因此在程序中不需要使用import语句就可以用String来实例化对象。String类主要用来处理程序初始化后不能改变的字符串。

    字符串的构造

    字符串常量是用双引号分隔的一系列java合法字符。在用赋值运算符进行字符串初始化时,JVM自动为每个字符串生成一个String类实例。字符串声明格式如下:

    String s;

    创建字符串可以使用String类的构造方法。

    s=new String("xjtu");

    也可以写成:

    s="xjtu";

    声明和实例化对象可以一步完成。

    利用String类提供的构造方法,可以生成空的字符串或者其他基本数据类型生成字符串对象。

    (1)利用下面的方法可以生成空的String实例。

    String str=new String();

    (2)在String类提供的构造方法中,可以由字符数组、字节数组以及字符串缓冲区来构造字符串,如下所示:

    char c1[]={'1','2','3','4'};

    char c2[]={'1','2','3','4','5'};

    String str1=new String(c1);

    String str2=new String(c2,0,3);

    System.out.print(str1);

    System.out.print(str1);

    结果:"1234"

    (3)利用字节数组生成字符串:

    byte c1[]={66,67,68};

    byte c2[]={65,66,67,68};

    String str1=new String(c1);

    String str2=new String(c2,1,3);

    System.out.print(str1);

    System.out.print(str1);

    结果均是BCD

    String类的常用方法

    String类提供了length(),charAt(),indexOf(),lastIndexOf(),getChars(),getBytes(),toCharArray()等方法。这些方法中按照用途分为字符串长度计算、字符串比较、字符串检索、字符串的截取、替换等方法。

    字符串长度计算

    使用String类中的方法length()方法可以获取一个字符串的长度。定义如下:

    public int length()

    该方法返回字符串的16-bit的Unicode字符的数量。例如:

    String s="xjtu";

    s.length()为4

    字符串比较

    字符串比较的方法有equals()、equalsIgnoreCase()、startsWith()、endsWith()、regionMatches()、compareTo()、compareToIgnoreCase()等方法。

    1)equals()和equalsIgnoreCase()方法

    equals()定义:

    public boolean equals(String s)

    该方法用于比较当前字符串对象的实体是否与参数指定的字符串s的实体相同。注意String类中的equals()方法和"=="的区别。

    equalsIgnoreCase()定义:

    public boolean equalsIgnoreCase(String s)

    字符串对象调用调用equalsIgnoreCase(String s)来比较当前对象是否与参数指定的字符串s相同,比较时忽略大小写。

    2)startsWith()和endsWith()方法

    字符串对象调用startsWith()和endsWith()方法分别判断当前字符串对象的前缀和后缀是否是参数指定的字符串s。

    定义:

    public boolean startsWith(String s)和public boolean endsWith(String s);

    例如:

    String str1="20150418";

    String str2="20140418";

    str1.startsWith("2015")的值是true;

    str2.endsWith("0418")的值是true;

    3)regionMatches()方法

    声明格式如下:

    public boolean regionMatches(int firstStart,String other,int otherStart,int length)和public boolean regionMatches(boolean b,int firstStart,String other,int otherStart,int length)

    从当前指定的字符串参数firstStart指定的位置开始处,取长度为length的一个子串,并将这个子串与参数other指定的一个子串进行比较。其中other指定的子串是是从参数otherStart指定的位置开始,从other中取长度为length的一个子串。如果两个子串相同就返回true,否则false。

    4)compareTo()和compareToIgnoreCase()方法

    声明格式:

    public int compareTo(String s)

    public int compareToIgnoreCase(String s)

    compareTo()方法按照字典顺序与参数s指定的字符串比较大小。如果当前字符串与s相同则返回0;如果如果大于s则返回正值,小于s返回负值。注意小写字母大于大写字母,而compareToIgnoreCase()方法忽略大小写。

    字符串检索

    搜索指定字符或字符串在字符串中出现的位置,用于字符或字符串在字符串中的定位。方法声明格式如下:

    public int indexOf(int ch)

    public int indexOf(int ch,int fromIndex)

    public int indexOf(String str)

    public int indexOf(String str,int fromIndex)

    上述四个重载方法分别用于在字符串中定位指定的字符和字符串,并且在方法种可以通过fromIndex来指定匹配的起始地址。如果没有检索到字符或字符串,该方法返回值是-1。

    String s="xjtuxjtu";

    int i;

    i=s.indexOf('x');//0

    i=s.indexOf("xjtu",0);//0

    i=s.indexOf('xjtu',4);//4

    另外,String类还提供字符串中的最后位置的定位,方法声明格式如下:

    public int lastIndexOf(int ch)

    public int lastIndexOf(int ch,int fromIndex)

    public int lastIndexOf(String str)

    public int lastIndexOf(String str,int fromIndex)

    上述4个重载方法分别用于在字符串中定位定位指定的字符和字符串最后出现的位置,并且在上述方法中可以通过fromIndex来指定匹配的起始位置。如果没有检索到字符或字符串,该方法返回值是-1。

    字符串截取

    在字符串中截取子字符串,其声明格式:

    public String substring(int beginIndex)

    该方法将获得一个当前字符串的子串,该子串是从当前字符串的beginIndex处截取到最后所得到的字符串。

    public String substring(int beginIndex,int endIndex)

    该方法将获得一个当前字符串的子串,该子串是从当前字符串的beginIndex处截取到endIndex-1结束处所得到的字符串。

    字符串的替换

    public String replace(char oldChar,char newChar)

    字符串对象s调用该方法可以获得一个串对象,这个串对象是用参数newChar指定的字符替换s中的oldChar指定对所有的字符而得到的字符串。

    public String replace(String old,String new)

    字符串对象s调用该方法可以获得一个串对象,这个串对象是用参数new指定的字符串替换s中的old指定对所有的字符串而得到的字符串。

    String s="xjtuxjtu";
    System.out.println(s.replace('x','s'));//sjtusjtu

    System.out.println(s.replace("xjtu","Tsinghua"));//TsinghuaTsinghua

     public String trim()

    一个字符串s通过调用方法trim()得到一个字符串对象,该字符串对象是s去掉前后空格后的字符串。

    其他的一些方法

    1)字符串大小写转换

    声明格式如下:

    public String toUpperCase(Local local)//仅对指定位置转换

    public String toUpperCase()

    public String toLowerCase(Local local)// 仅对指定位置转换

    public String toLowerCase()

    2)转换为字符串数组

    声明格式如下:

    public char[] toCharArray()

    该方法用于将字符串转换为字符串数组。该方法的返回值类型为字符数组,如下

    String str=new String("I love xjtu");

    char[] ch;

    ch=str.toCharArray();

    System.out.print(ch);//I love xjtu

    3)字符串到字符数组之间的转换

    声明格式:

    getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)

    该方法用于将字符串中的字符内容复制到字符数组中。其中srcBegin为复制的起始位置,srcEnd-1为复制的终止位置、字符串数值dst为目的字符数组,dstBegin为目的字符数组的复制的起始位置。复制的时候会覆盖原来位置数据。

    4)连接两个字符串

    声明格式如下:

    public String concat(String str)

    该方法用于将两个字符串连接在一起,与字符串的"+" 操作符功能相同。

  • 相关阅读:
    DataWindow修改的单元格文字颜色改变
    DataWindow奇偶行颜色显示不同
    Retrieve时不清除DataWindow原有数据
    Camera拍照声设定
    使用Log.isLoggable方法
    Android Audio遇到播放无声时的分析
    耳机jack构造及在应用时可能出现的问题
    【Android】使用persist属性来调用脚本文件
    [Android][Audio] audio_policy.conf文件分析
    Android4.4 耳机检测分析
  • 原文地址:https://www.cnblogs.com/weink1215/p/4436969.html
Copyright © 2011-2022 走看看