Java的常用类String 05

来源:互联网 发布:linux 启动器 编辑:程序博客网 时间:2024/05/18 01:10

  

一、String类的特点:

字符串对象一旦被初始化就不会被改变

例如:

public class StringDemo{

       public static void main(String[] args){

             String s = "abc";

              s = "nba";

            System.out.println("s = " + s);

      }

}


重点:

1.引用的s放在栈中
2.字符串常量放在常量池(字符串常量池)中,第一次创建放入池中,第二次使用直接把引用指向池中已有的数据。

3.newString("xxx")创建的 数据放在堆中,每次new都会在堆中创建一块内存存放数据。

4. String类复写了Object中的equals方法,建立了String类自己的判断字符串对象是否相同的依据。只比较字符串内容,不比较地址。


二、常用构造方法摘要

1.String()
 
初始化一个新创建的 String 对象,它表示一个空字符序列。


2.String(byte[] bytes) 
 使用平台的默认字符集解码指定的bety数组,构造一个新的 String。


3. String(byte[] bytes,int offset, int length)
 
通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的String。新 String 的长度是字符集的函数,因此可能不等于该子数组的长度。

bytes - 要解码为字符的 byte

offset - 要解码的第一个 byte 的索引

length - 要解码的 byte 数


4.String(char[] value)
 
分配一个新的 String,它表示当前字符数组参数中包含的字符序列。


5.String(String original)
  初始化一个新创建的 String 对象,表示一个与该参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的一个副本。


三、String类部分方法

1、获取:

a.

获取字符串中字符的个数(长度)

int length();

b.

根据位置获取字符

    char charAt(int index);

c.

根据字符获取在字符串中的位置

     int indexOf(int ch);

    indexO方法参数f类型为int是为了既可以支持字符,也可以支持字符在ASCII码中对应的数字。

d.

从指定位置开始查找ch第一次出现的位置。

     int indexOf(int ch,int fromIndex);

    int indexOf(String str);

     int indexOf(String str,int fromIndex);

  根据字符串获取在字符串中第一次出现的位置(从后开始找)。

    int lastIndexOf(int ch);

    int lastIndexOf(int ch,int fromIndex);

     int lastIndexOf(String str);

    int lastIndexOf(String str,int fromIndex);

   P.S: 可以根据-1,来判断该字符或者字符串是否存在。

e.

获取字符串中的一部分字符串,也叫子串。

   String substring(int beginIndex,intendIndex);

    String substring(int beginIndex);

例如:

public class StringMethodDemo{       public static void main(String[] args){            StringMethodDemo();      }       public static void StringMethodDemo(){            String s = "abcdae";            System.out.println( "substring:" + s.substring(2));            System.out.println( "substring:" + s.substring(2,4));      }}

2、转换:

a.

将字符串变成字符串数组(字符串的切割)

    String[] split(String regex);涉及到正则表达式。

b.

将字符串变成字符数组

           char[] toCharArray();

将字符串变成字节数组

     char[] getBytes();

c.

将字符串中的字母转成大小写

    String toUpperCase();大写

    String toLowerCase();小写

d.

将字符串中的内容进行替换

     String replace(char oldCh,char newCh);

     String replace(String s1,String s2);

 P.S: replace方法如果没有找到要替换的内容,则返回的还是原字符串。

e.

去除字符串两端空格

    String trim();


3、判断:

a.

两个字符串内容是否相同呢?

    boolean equals(Object obj);

    boolean equalsIgnoreCase(String str);忽略大小写比较字符串内容。

b.

字符串中是否包含指定字符串

     boolean contains(String str);

c.

字符串是否以指定字符串开头,是否以指定字符串结尾

     boolean startsWith(String str);

     boolean endsWith(String str);

d.

    int compareTo(String str);  

如果参数字符串等于此字符串,则返回值0;如果此字符串按字典顺序小于字符串参数,则返回一个小于0 的值;如果此字符串按字典顺序大于字符串参数,则返回一个大于0的值。


3、返回字符串对象的规范化表示形式

  String intern();

    当调用intern方法时,如果池已经包含一个等于此String对象的字符串(用equals(Object)方法确定),则返回池中的字符串。否则,将此String对象添加到池中,并返回此String对象的引用。


四、练习:

例1:

/*2,将一个字符串进行反转,将字符串中指定部分进行反转,"abcdefg";思路:1,曾经学过的对数组的元素进行反转。2.将字符串变成数组,对数组反转。3,将反转后的数组变成字符串。4,只要将反转的部分的开始和结束位置作为参数传递即可,*/class  StringTest2{public static void sop(String str){System.out.println(str);}public static void main(String[] args) {String s = "0123456";sop("("+s+")");sop("("+reverseString(s)+")");}private static void reverse(char[] arr,int x,int y){for (int start = x,end = y-1; start<end; start++,end--){swap(arr,start,end);}}private static void swap(char[] arr,int x,int y){char temp = arr[x];arr[x] = arr[y];arr[y] = temp;}public static String reverseString(String s,int start,int end){//字符串变数组char[] chs = s.toCharArray();//反转数组reverse(chs,start,end);//将数组变成字符串。return new String(chs);}<pre name="code" class="java">}

例2:

/*3,获取一个字符串在另一个字符串中出现的次数。“abkkdsfgadkksdkk”思路:1,定义一个计数器2,获取kk第一次出现的位置。3,从第一次出现位置后剩余的字符串中继续获取kk出现的位置每获取一次就计数一次。4,当获取不到时,计数完成。*/class  StringTest3{public static void sop(String str){System.out.println(str);}public static void main(String[] args) {String s = "abkkdsfgadkksdkk";sop("count="+getSubCount_2(s,"kk"));}public static int getSubCount(String str,String key) {int count = 0;int index = 0;while ((index = str.indexOf(key))!=-1){sop("str="+str);str = str.substring(index+key.length());count++;}return count;}public static int getSubCount_2(String str,String key) {int count = 0;int index = 0;while ((index = str.indexOf(key,index))!=-1){sop("index="+index);index = index + key.length(); count++;}return count;}}



例3:

/*4,获取两个字符串中最大相同字串,第一个动作:将短的那个串进行长度依次递减的字串打印,"dasfasdahelloded""hkvmdhellodgf"思路:1,将短的那个字串按照长度递减的方式获取到。2,将每获取到的字串去长串中判断是否包含。如果包含,已经找到!*/class  StringTest4{public static void sop(String str){System.out.println(str);}public static void main(String[] args) {String s1 = "dasfasdahelloed";String s2 = "hkvmdhellogf";sop(getMaxSubString(s1,s2));}public static String getMaxSubString(String s1,String s2){String max = "",min = "";max = (s1.length()>s2.length())?s1:s2;min = (max==s1)?s2:s1;sop("max="+max+".....min="+min);for (int x = 0; x<min.length(); x++){for (int y = 0,z = min.length()-x; z!=min.length()+1; y++,z++){String temp = min.substring(y,z);//sop(temp);if (max.contains(temp)){return temp;}}}return "";}}




0 0
原创粉丝点击