zoukankan      html  css  js  c++  java
  • Java字符串处理

    一、概述

    Java是以String类型的对象来实现字符串。String是一个类,当创建一个String对象后,所创建的字符串是不能改变的。在需要使用可修改的字符串时,Java提供两个选择—StringBuffer和StringBuilder。注:声明为String引用的变量在任何时候都可以改变,以指向一些其他的String对象。

    二、String构造函数

    String s1="hello"与String s2=new String("hello")的区别:

    String类在内存中管理一个字符串常量池(常量池的一部分),池中所有相同的字符串常量被合并,只占用一个空间。

    String s1=”hello”,先看池中有没有hello,没有就创建一个hello字符串对象。即采用此方法创建0或者1个对象。

    String s2=new String(“hello”),这里先在池中创建一个hello对象,同s1,然后new String()时,将hello对象复制到堆heap中,s2指向堆中的hello。采用此种方法时创建1或2个对象(当池中有hello时,创建一个对象)。

    内存图如下:

    三、String类的方法函数

    String类有很多方法,这里只介绍几种常见的函数:

    1、字符串的长度:

    int length();

    2、字符串转换

    每个类都会实现toString方法,因为它是由Obect定义的,对于大多数的自创建的类来说,通常要重写toString()并提供自己的字符串表达式。

    eg:

     1 class Box 
     2 {
     3     int x;
     4     int y;
     5     Box(int x,int y)
     6     {
     7         this.x=x;
     8         this.y=y;
     9     }
    10     public String toString()
    11     {
    12         return "test_x"+"-----"+x+"
    "+"test_y"+"-----"+y;
    13     }
    14 }
    15 public class Tset
    16 {
    17     public static void main(String[] args)
    18     {
    19             Box b=new Box(1,3);
    20             System.out.println(b);
    21     }
    22 
    23 }

    显示结果为:

    疑问:看了一下println的源码,不知道此处输出结果为何是如此?

     1 private void write(String s) {
     2         try {
     3             synchronized (this) {
     4                 ensureOpen();
     5                 textOut.write(s);
     6                 textOut.flushBuffer();
     7                 charOut.flushBuffer();
     8                 if (autoFlush && (s.indexOf('
    ') >= 0))
     9                     out.flush();
    10             }
    11         }
    12         catch (InterruptedIOException x) {
    13             Thread.currentThread().interrupt();
    14         }
    15         catch (IOException x) {
    16             trouble = true;
    17         }
    18     }

    3、字符抽取

    charAt():  返回指定索引处的 char 值。
    
    getChars():一次抽取多个字符。
    
    getBytes():将字符存储在字节数组里。

    4、字符串比较

    equals()和==:equals()方法是比较String对象中的字符,“==”运算符是比较两个对象是否引用同一实例。

    eg:

     1 public class Test
     2 {
     3     public static void main(String[] args)
     4     {
     5         String s1=new String("hello");//创建2个对象,一个pool和一个堆里面 
     6         String s2="hello";//创建1个对象,s2指向pool里面的"hello"对象 
     7         String s3="hello";//创建0个对象,指向s2指向pool里面的那个对象 
     8         String s4=s2;//创建0个对象,指向s2,s3指向pool里面的那个对象 
     9         String s5=new String("hello");//创建1个对象在堆里面,注意,与s1没关系 
    10 
    11         System.out.println(s2=="hello");//true 
    12         System.out.println(s2==s3);//true,因为指向的都是pool里面的那个"hello" 
    13         System.out.println(s2==s4);//true,同上,那么s3和s4...:) 
    14         System.out.println(s1==s5);//false,很明显,false 
    15         System.out.println(s1==s2);//false,指向的对象不一样
    16         System.out.println(s1=="hello");//false
    17         System.out.println("---------------"); 
    18         s1=s2; 
    19         System.out.println(s1=="hello");//true
    20     }
    21 }

    执行结果:

    boolean startsWith(String prefix):判断给定的String是否以特定的字符串开始。
    
    boolean startsWith(String prefix,int toffset):判断此字符从指定索引开始的字符串是否以指定的前缀开始。
    
    endsWith():判断给定的String是否是以特定的字符串结束。
    
    compareTo():用于比较字符串大小,并考虑大小写字母。
    
    compareIgnoreCase():比较字符串大小并忽略大小写字母。

    5、查找字符串

    isdexOf():查找字符或子串第一次出现的位置
    
    lastIndexOf():查找字符或者子串最后出现的位置

    6、更改字符串

    因为String对象是不能改变的,当需要更改一个字符串时,就必须将他复制到一个StringBuffered中,后者使用下列方法,在更改后会构造一个新的字符串副本。

    substring(int startIndex):返回从startIndex开始到调用字符串结束的子串的一个副本
    
    substring(int startIndex,int endIndex),指定起点和终点,返回这中间的字符串,不包括终点。
    
    concat():连接两个字符串,与“+”运算符相同。
    
    replace(char originial,char replacement):用一个字符在调用字符串中所有出现另一个字符的地方进行替换。
    
    replace(CharSequence originial,CharSequence replacement):用一个字符串序列替换另一个字符串序列。
    
    trim():返回调用字符串对象的一个副本,但是所有起始和结尾的空白符都被删除了(字符中间的空白符未被删除)。

    7、改变字符串中字符的大小写

    toLowerCase():将所有字符改为小写
    
    toUpperCase():将所有字符改为大写

    四:StringBuffer类

    StringBuffer是String的一个对等类,提供了字符串的许多功能,可增长、可改写。

    这里只介绍几个StringBuffer类的方法函数:

    1、append():将任何数据类型的字符串表示连接到调用的StringBuffer对象的末尾。

    当定String对象使用“+”运算符时,经常要调用append方法。

    StringBuffer s=new StringBuffer("hello");
        System.out.print(s.append(" world"));

    2、inser(int index,String str):将一个字符串插入到另一个字符串中。

    3、reverse():颠倒StringBuffer对象中的字符

    4、delete(int startIndex,int endIndex),delete(int loc):调用对象中删除一串字符。

    五、StringBuilder类

    除了一个重要的区别之外,与StringBuffer等同,这个区别是他不是同步的,意味着他不是线程安全的,其优势是更快的性能,在使用多线程时必须使用StringBuffer。

  • 相关阅读:
    HDU1007
    DFA
    netstat
    Sangfor
    JS 基础逻辑关系
    正则表达式
    JS中的DOM
    HTML、CSS、JS面试题
    JS作用域和作用域链
    JS String与正则表达式
  • 原文地址:https://www.cnblogs.com/xujian2014/p/4437748.html
Copyright © 2011-2022 走看看