美文网首页
字符串的操作

字符串的操作

作者: 飘飘哥 | 来源:发表于2016-11-18 10:03 被阅读0次

    一.Java字符串类基本概念

    在JAVA语言中,字符串数据实际上由String类所实现的。Java字符串类分为两类:一类是在程序中不会被改变长度的不变字符串;二类是在程序中会被改变长度的可变字符串。Java环境为了存储和维护这两类字符串提供了StringStringBuffer两个类。

    一、创建字符串

    例:Stringstr=new("This is a String");

    或者Stringstr="This is a String";

    二、得到字符串对象的有关信息

    1.通过调用length()方法得到String的长度.

    例:

    Stringstr="Thisis a String";

    intlen =str.length();

    2.StringBuffer类的capacity()方法与String类的length()的方法类似,但是她测试是分配给StringBuffer的内存空间的大小,而不是当前被使用了的内存空间。

    3.如果想确定字符串中指定字符或子字符串在给定字符串的位置,可以用indexOf()lastIndexOf()方法。

    Stringstr="Thisis a String";

    Int index1 =str.indexOf("i");//index=2

    Intindex2=str.indexOf(‘i‘,index+1);   //index2=5

    Intindex3=str.lastIndexOf("I");   //index3=15

    Intindex4=str.indexOf("String");  //index4=10

    三、String对象的比较和操作

    1.String对象的比较

    String类的equals()方法用来确定两个字符串是否相等。

    Stringstr="Thisis a String";

    Boolean result=str.equals("This is another String");

    //result=false

    2.String对象的访问

    A、方法charAt()用以得到指定位置的字符。

    Stringstr="Thisis a String";

    charchr=str.charAt(3);//chr="i"

    B、方法getChars()用以得到字符串的一部分字符串

    publicvoidgetChars(intsrcBegin,intsrcEnd,char[]dst,intdstBegin)

    Stringstr="Thisis a String";

    Char chr =newchar[10];

    Str.getChars(5,12,chr,0);//chr="isa St"

    C、subString()是提取字符串的另一种方法,它可以指定从何处开始提取字符串以及何处结束。

    3.操作字符串

    A、replace()方法可以将字符串中的一个字符替换为另一个字符。

    Stringstr="Thisis a String";

    Stringstr1=str.replace(‘T‘,‘t‘);//str1="thisis a String"

    B、concat()方法可以把两个字符串合并为一个字符串。

    Stringstr="Thisis a String";

    Stringstr1=str.concat("Test");//str1="Thisis a String Test"

    C、toUpperCase()toLowerCase()方法分别实现字符串大小写的转换。

    Stringstr="THISIS A STRING";

    Stringstr1=str.toLowerCase();//str1="thisis a string";

    D、trim()方法可以将字符串中开头和结尾处的空格去掉.

    Stringstr="Thisis a String   ";

    Stringstr1=str.trim();//str1="This is a String"

    E、String类提供静态方法valueOf(),它可以将任何类型的数据对象转换为一个字符串。如

    System.out.println(String,ValueOf(math,PI));

    四、修改可变字符串

    StringBuffer类为可变字符串的修改提供了3种方法,在字符串中间插入和改变某个位置所在的字符。

    1.在字符串后面追加:用append()方法将各种对象加入到字符串中。

    2.在字符串中间插入:用insert()方法。例

    StringBuffer str=newStringBuffer("Thisis a String");

    Str.insert(9,"test");

    System.out.println(str.toString());

    这段代码输出为:Thisis a test String

    3.改变某个位置所在的字符,用setCharAt()方法。

    StringBuffer sb =new StringBuffer("aaaaaa");

    sb.setCharAt(2, “b”); // 结果aabaaa

    二.字符串分割

    1.利用字符串类的split方法进行分割

    /** *//**利用字符串的split方法进行分割

    *@paramstr待分割的字符串

    *@paramsdelimiter分割符

    *@return

    */

    publicString[] splitString(String str,String sdelimiter)...{

    String[] array=str.split(sdelimiter);

    returnarray;

    }

    2.利用StringTokenizer来进行字符串分割

    /** *//**利用StringTokenizer来进行字符串分割

    *@paramstr待分割的字符串

    *@paramsdelimiter分割符

    *@return

    */

    publicString[] useStringTokenizer(String str,String sdelimiter)...{

    StringTokenizer token=newStringTokenizer(str,sdelimiter);

    String[] array=newString[token.countTokens()];

    inti=0;

    while(token.hasMoreTokens())...{

    array[i]=token.nextToken();

    i++;

    }

    returnarray;

    }

    三.字符串数组排序

    /** *//**对字符串数组进行排序

    *@paramstr原始字符串数组

    *@paramflag flag=0:顺序排序flag=1:倒序排序

    *@return排序后的字符串数组

    */

    publicString[] sort(String[] str,intflag)...{

    if(str==null||str.length==0)

    thrownewIllegalArgumentException();

    String temp=str[0];

    //顺序排列,即从小到大

    if(flag==0)...{

    for(inti=0;i

    for(intj=i+1;j

    if(str[i].compareTo(str[j])>0)...{

    temp=str[i];

    str[i]=str[j];

    str[j]=temp;

    }

    }

    }

    }

    elseif(flag==1)...{//倒序排列

    for(inti=0;i

    for(intj=i+1;j

    if(str[i].compareTo(str[j])<0)...{

    temp=str[i];

    str[i]=str[j];

    str[j]=temp;

    }

    }

    }

    }

    returnstr;

    }

    四.使用Hashtable对字符串进行碰撞

    利用hashtable对字符串进行过滤,两个字符数组之间的比较,对字符串数组进行过滤

    1.在一些字符串数组中,常会有重复的记录,比如手机号码,我们可以通过Hashtable来对其进行过滤

    publicString[] checkArray(String[] str)...{

    Hashtable hash=newHashtable();

    for(inti=0;i

    if(!hash.containsKey(str[i]))

    hash.put(str[i], str[i]);

    }

    Enumeration enumeration=hash.keys();

    String[] str_new=newString[hash.size()];

    inti=0;

    while(enumeration.hasMoreElements())...{

    str_new[i]=enumeration.nextElement().toString();

    i++;

    }

    returnstr_new;

    }

    示例:

    String[]mobile={"13811071500","13811071500","13811071501","13811071503","13811071501"};

    mobile=checkArray(mobile);

    for(int i=0;i

    System.out.println(mobile[i]);

    输出结果为:

    13811071503

    13811071501

    13811071500

    2.A,B均为字符串数组,找出在A中存在,而在B中不存在的字符串

    public String[] compareArray(String[] A,String[] B){

    Hashtable hash=newHashtable();

    Hashtablehash_new=new Hashtable();

    for(int i=0;i

    hash.put(B[i], B[i]);

    for(int i=0;i

    if(!hash.containsKey(A[i]))

    hash_new.put(A[i], A[i]);

    }

    String[] C=new String[hash_new.size()];

    int i=0;

    Enumeration enumeration=hash_new.keys();

    while(enumeration.hasMoreElements()){

    C[i]=enumeration.nextElement().toString();

    i++;

    }

    return C;

    }

    示例:

    String[] mobile1={"13811071500","13811071501","13811071502","13811071503","13811071504"};

    String[]mobile2={"13811071500","13811071505","13811071502","13811071506","13811071504"};

    String[]mobile3=compareArray(mobile1,mobile2);

    for(int i=0;i

    System.out.println(mobile[i]);

    输出结果:

    13811071503

    13811071501

    存在的问题:

    每次都是倒序,可以再对程序稍加改动,变成正序。

    3.将一个字符串数组中某一个特定的字符串过滤掉

    /** *//**检验一个字符串数组,若包含某一特定的字符串,则将该字符串从数组中删

    除,返回剩余的字符串数组

    *@paramstr_array字符串数组

    *@paramstr_remove待删除的字符串

    *@return过滤后的字符串

    */

    publicString[] removeStrFromArray(String[] str_array,String

    str_remove)...{

    Hashtable hash=newHashtable();

    for(inti=0;i

    if(!str_array[i].equals(str_remove))

    hash.put(str_array[i], str_array[i]);

    }

    //生成一个新的数组

    String[] str_new=newString[hash.size()];

    inti=0;

    Enumeration enumeration=hash.keys();

    while(enumeration.hasMoreElements())...{

    str_new[i]=enumeration.nextElement().toString();

    i++;

    }

    returnstr_new;

    }

    相关文章

      网友评论

          本文标题:字符串的操作

          本文链接:https://www.haomeiwen.com/subject/pcvjpttx.html