String和StringBuffer详解

来源:互联网 发布:excel数据导入表格 编辑:程序博客网 时间:2024/06/04 01:27

一.String字符串的创建

    创建String字符串最简单的方式是使用字符串文本:
<span style="white-space:pre"></span>String str="hello java|面向对象程序设计";
    除直接为变量赋值外,还可以使用new操作符来声明字符串:
<span style="white-space:pre"></span>String stt2=new String("hello java|面向对象程序设计"

    还有另外的两种数组声明方式:

char str[]={'h','e','l','l','o'};

 <span style="white-space:pre"></span>String str[]={ "hello","java","面向对象","123"};

二.String类型字符串的常用操作

   (1).length方法:该方法用于计算字符串的长度,方法返回字符串中的字符数
<span style="font-family:SimSun;font-size:18px;">     <span style="white-space:pre"></span>String str="Java";     <span style="white-space:pre"></span>sop(str.length());//输出4</span>
   (2).getChars方法:该方法用于将字符串的字符复制到字符数组去,一般形式如下
<span style="white-space:pre"></span>void getChar(int srcBegin,int srcEnd,char[] std.int dstBegin)
    举例如下:
<span style="white-space:pre"></span>String srcStr="java";<span style="white-space:pre"></span>char dstChch[]=new char[20];<span style="white-space:pre"></span>int n=srcStr.length();<span style="white-space:pre"></span>srcStr.getChars(0,n.dstCh,o);
   (3).isLowerCase、isUpperCase:这两个方法是用来判断字符串中的字符的大小写,注意是单个字符,在此不举例;
   (4).tolowercase、touppercase:这两个方法是用来对字符串中的字符的大小写进行转换,注意也是单个字符;
   (5).compareTo;该方法用于字符串的比较,,注意compareto的方法是对字符串从小到大,这个比较的其实是ASCII码;
   (6).concat:该方法用于字符串连接,并返回新的字符串,一般形式如下:
<span style="white-space:pre"></span>String concat(String str);
    举例如下:
<span style="white-space:pre"></span>String str="java_",str2="hi",str3;       <span style="white-space:pre"></span>str3=str1.concat(str2);       <span style="white-space:pre"></span>sop(str3);//输出java_hi</span>
   (7).substring:该方法用于提取调用方法的字符串中的子串,并返回新的字符串,一般格式如下:
<span style="white-space:pre"></span>String substring(int begin,int end)
    例如:
<span style="white-space:pre"></span>Srting str="Java_hi";       str2=str1.substring(0,3);       sop(str2);//输出Java;
   (8).repalce:该方法用于替换字符串中的某个字符,返回替换后的新字符串,一般格式如下
<span style="white-space:pre"></span>String replace(char oldChar,char newChar);
   (9).indexof,lastindexof:这两个方法用于对字符串建立索引,返回字符串的位置,一般格式如下:
<span style="white-space:pre"></span>int indexOf(String str);<span style="white-space:pre"></span>int lastIndexOf(String str);
    例如:
<span style="white-space:pre"></span>A.indexOf(B);或者A.lastIndexOf(B);//区别是从前向后还是从后向前。

三.SringBuffer对象的创建

与String不同,StringBuffer的创建方式只有一种,如果字符串的内容经常改变,用StringBuffer
StringBuffer 字符串名称=new StringBuffer(<对象序列>);
<span style="white-space:pre"></span>StringBuffer name=new StringBuffer("张三");

四.StringBuffer类的方法

(1).capacity:该方法用来计算StringBuffer的容量,返回容量大小的整型值,一般格式为
<span style="white-space:pre"></span>int capacity();
例如:
<span style="white-space:pre"></span>StringBuffer insert(int offset,char ch);
<span style="white-space:pre"></span>x=A.capacity();
(2).append:该方法讲指定的字符串的内容连接到指定对象的后面,并返回连接后的对象,一般格式为:
<span style="white-space:pre"></span>StringBuffer append(String str)
例如:
<span style="white-space:pre"></span>StringBuffer  str=new StringBuffer"java-";'<span style="white-space:pre"></span>StringBuffer  str1=new StringBuffer"hello";<span style="white-space:pre"></span>StringBuffer  str2;<span style="white-space:pre"></span>str2=str1.append(str);
(3).insert:该方法将指定的字符插入到StringBuffer的offset处,并返回修改后的对象
(4).delete:该方法用于将StringBuffer的一部分内容删掉,并将修改后的字符串返回
<span style="white-space:pre"></span>StringBuffer delete(int start,int end);
(5).reverse:该方法用于颠倒字符串,把最后一个字符变成第一个字符,以此类推,一般格式如下:
<span style="white-space:pre"></span>StringBuffer reverse();
例如
<span style="white-space:pre"></span>StringBuffer str=new  StringBuffer("I LOVE JAVA");<span style="white-space:pre"></span>StringBuffer str2=str.reverse();<span style="white-space:pre"></span>sop(str2);//输出AVAJ EVOL I;    </span>


0 0
原创粉丝点击