photoshop cs6 w10:Z)JAVA?字符串使用

来源:百度文库 编辑:中财网 时间:2024/04/29 04:44:36
 字符串的表示

   Java语言中,字符串可以分为字符串常量和字符串对象,字符串常量通过String str = "AAA";声明及初始化,
字符串对象通过new操作来初始化。(先这么简单理解,字符串常量也是字符串对象,只是空间分配上有些许差异,后边再解释)

   1.字符串常量

  字符串常量是用双引号括住的一串字符:"Hello World!"

   2.String表示字符串常量

  用String表示字符串:
   String( char chars[ ] );
   String( char chars[ ], int startIndex, int numChars );
   String( byte ascii[ ], int hiByte );
   String( byte ascii[ ], int hiByte, int startIndex, int numChars );
   String使用示例:
  String s=new String() ; 生成一个空串

  下面用不同方法生成字 符串"abc":
  char chars1[]={’a’,’b’,’c’};
   char chars2[]={’a’,’b’,’c’,’d’,’e’};
   String s1=new String(chars1);
  String s2=new String(chars2,0,3);
  byte ascii1[]={97,98,99};
  byte ascii2[]={97,98,99,100,101};
  String s3=new String(ascii1,0);
   String s4=new String(ascii2,0,0,3);
3.用StringBuffer表示字符串

  StringBuffer( );
  StringBuffer( int len ); StringBuffer( String s );

★字符串的访问

   1.类String中提供了length( )、charAt( )、indexOf( )、 lastIndexOf( )、getChars( )、getBytes( )、toCharArray( )等方法。

   ◇ public int length() 此方法返回字符串的字符个数
   ◇ public char charAt(int index) 此方法返回字符串中index位置上的字符,其中index 值的 范围是 0~length-1
  ◇ public int indexOf(int ch)
     public lastIndexOf(in ch)
  
  返回字符ch在字符串中出现的第一个和最后一个的位置
  ◇ public int indexOf(String str)
    public int lastIndexOf(String str)
  返回子串str中第一个字符在字符串中出现的第一个和最后一个 的位置
  ◇ public int indexOf(int ch,int fromIndex)
    public lastIndexOf(in ch ,int fromIndex)
  返回字符ch在字符串中位置fromIndex以 后出现的第一个和最后一个的位置
  ◇ public int indexOf(String str,int fromIndex)
    public int lastIndexOf(String str,int fromIndex)
  返回子串str中的第一个字符在字符串中位置fromIndex后出现的第一个和最后一个的位置。
   ◇ public void getchars(int srcbegin,int end ,char buf[],int dstbegin)
   srcbegin 为要提取的第一个字符在源串中的位置, end为要提取的最后一个字符在源串中的位置,字符数组buf[]存放目的字符串, dstbegin 为提取的字符串在目的串中的起始位置。
   ◇public void getBytes(int srcBegin, int srcEnd,byte[] dst, int dstBegin)
  参数及用法同上,只是串中的字符均用8位表示。

  2.类StringBuffer提供了 length( )、 charAt( )、getChars( )、capacity()等方法。

  方法capacity()用来得到字符串缓冲区的容量,它与方法length()所返回的值通常是不同的。
★修改字符串
修改字符串的目的是为了得到新的字符串,类String和类 StringBuffer都提供了相应的方法。有关各个方法的使用,参考java 2 API。

  1.String类提供的方 法:

   concat( )
   replace( )
   substring( )
    toLowerCase( )
   toUpperCase( )

   ◇ public String contat(String str);
  用来将当前字符串对象与给定字符串str连接起来。
  ◇ public String replace(char oldChar,char newChar);
  用来把串中出现的所有特定 字符替换成指定字符以生成新串。
  ◇ public String substring(int beginIndex);
  public String substring(int beginIndex,int endIndex);
  用来得到字符串中指定 范围内的子串。
  ◇ public String toLowerCase(); 把串中所有的字符变成小写。
  ◇ public String toUpperCase();
  把串 中所有的字符变成大写。

  2.StringBuffer类提供的方法:

  append( )
   insert( )
  setCharAt( )

  如果操作后的字符超出已分配的缓冲区,则系统会自动为它分配额外 的空间。
  ◇ public synchronized StringBuffer append(String str);
   用来在已有字符串末尾添加一个字符串str。
   ◇ public synchronized StringBuffer insert(int offset, String str);
   用来在字符串的索引offset位置处插入字符串str。
   ◇ public synchronized void setCharAt(int index,char ch);
  用来设置指定索引 index位置的字符值。

  注意:String中对字符串的操作不是对源操作串对象本身进行的,而是对新生成的一个源操作串对象的拷贝进行的,其操作的结果不影响源串。

  相反,StringBuffer中对字符串的连接操作是对源串本身进行的,操作之后源串的值发生了变化,变成连接后的串。

其它操作

   1.字符串的比较

   String中提供的方法:
  equals( )和equalsIgnoreCase( )
  它们与运算符’= =’实现的比较是不同的。运算符’= =’比较两个对象是否引用同一个实例,而equals( )和equalsIgnoreCase( )则比较  两个字符串中对应的每个字符值是否相同。

   2.字符串的转化

  java.lang.Object中提供了方法toString( )把对象转化为字符串。

   3.字符串"+"操作

  运算符’+’可用来实现字符串的连接:
   String s = "He is "+age+" years old.";
  其他类型的数据与字符串进行"+"运算时,将自动转换成 字符串。具体过程如下:
   String s=new StringBuffer("he is").append(age).append("years old").toString();

  注意:除了对运算符"+"进行了重载外,java不支持其它运算符的重载。对字符串常量的分析1. 首先String不属于8种基本数据类型,String是一个对象。

   因为对象的默认值是 null,所以String的默认值也是null;但它又是一种特殊的对象,有其它对象没有的一些特性。

  2. new String()和new String(“”)都是申明一个新的空字符串,是空串不是null;

  3. String str=”kvill”;
String str=new String (“kvill”);的区别:

   在这里,我们不谈堆,也不谈栈,只先简单引入常量池这个简单的概念。

  常量池(constant pool)指的是在编译期被确定,并被保存在已编译的.class文件中的一些数据。它包括了关于类、方法、接口等中的常量,也包括字符串 常量。

  看例1:

String s0=”kvill”;
String s1=”kvill”;
String s2=”kv” + “ill”;
System.out.println( s0==s1 );
System.out.println( s0==s2 );
  结果为:

true
true
  首先,我们要知道Java会确保一个字符串常量只 有一个拷贝。

  因为例子中的s0和s1中的”kvill”都是字符串常量,它们在编译期就被确定了,所以s0==s1为true;而”kv”和”ill”也都是字符串常量,当一个字符串由多个字符串常量连接而成时,它自己肯定也是字符串常量,所以s2也同样在编译期就被解析为一个字符串常量,所以s2也是常量池中”kvill”的一个引用。

   所以我们得出s0==s1==s2;

  用new String() 创建的字符串不是常量,不能在编译期就确定,所以new String() 创建的字符串不放入常量池中,它们有自己的地址空间。

  看例2:

String s0=”kvill”;
String s1=new String(”kvill”);
String s2=”kv” + new String(“ill”);
System.out.println( s0==s1 );
System.out.println( s0==s2 );
System.out.println( s1==s2 );
  结果为:

false
false
false
  例2中s0还是常量池中”kvill”的应用,s1因为无法在编译期确定,所以是运行时创建的新对象”kvill”的引用,s2因为有后半部分new String(“ill”)所以也无法在编译期确定,所以也是一个新创建对象”kvill”的应用;明白了这些也就知道为何得出此结果了。

  4. String.intern():

  再补充介绍一点:存在于.class文件中的常量池,在运行期被JVM装载,并且可以扩充。String的intern()方法就是扩充常量池的一个方法;当一个String实例str调用intern()方法时,Java查找常量池中是否有相同Unicode的字符串常量,如果有,则返回其的引用,如果没有,则在常量池中增加一个Unicode等于str的字符串并返回它的引用;看例3就清楚了

   例3:

String s0= “kvill”;
String s1=new String(”kvill”);
String s2=new String(“kvill”);
System.out.println( s0==s1 );
System.out.println( “**********” );
s1.intern();
s2=s2.intern(); //把常量池中“kvill”的引用赋给s2
System.out.println( s0==s1);
System.out.println( s0==s1.intern() );
System.out.println( s0==s2 );
  结果为:

false
**********
false //虽然执行了s1.intern(),但它的返回值没有赋给s1
true //说明s1.intern()返回的是常量池中”kvill”的引用
true
  最后我再破除一个错误的理解:

  有人说,“使用String.intern()方法则可以将一个String 类的保存到一个全局String表中,如果具有相同值的Unicode字符串已经在这个表中,那么该方法返回表中已有字符串的地址,如果在表中没有相同值的字符串,则将自己的地址注册到表中“如果我把他说的这个全局的String表理解为常量池的话,他的最后一句话,“如果在表中没有相同值的字符串,则将自己的地址注册到表中”是错的:

  看例4:

String s1=new String("kvill");
String s2=s1.intern();
System.out.println( s1==s1.intern() );
System.out.println( s1+" "+s2 );
System.out.println( s2==s1.intern() );
  结果:

false
kvill kvill
true
  在这个类中我们没有声名一个”kvill”常量,所以常量池中一开始是没有”kvill”的,当我们调用s1.intern()后就在常量池中新添加了一个”kvill”常量,原来的不在常量池中的”kvill”仍然存在,也就不是“将自己的地址注册到常量池中”了。

   s1==s1.intern()为false说明原来的“kvill”仍然存在;

  s2现在为常量池中“kvill”的地址,所以 有s2==s1.intern()为true。

  5. 关于equals()和==:

  这个对于String简单来说就是比较两字符串的Unicode序列是否相当,如果相等返回true;而==是比较两字符串的地址是否相同,也就是是否是同一个字符串的引用。

   6. 关于String是不可变的

  这一说又要说很多,大家只要知道String的实例一旦生成就不会再改变了,比如 说:String str=”kv”+”ill”+” “+”ans”;
就是有4个字符串常量,首先”kv”和”ill”生成了”kvill” 存在内存中,然后”kvill”又和” “ 生成 ”kvill “存在内存中,最后又和生成了”kvill ans”;并把这个字符串的地址赋给了str,就是因为String的“不可变”产生了很多临时变量,这也就是为什么建议用StringBuffer的原因了,因为StringBuffer是可改变的另外参考此程序:
public class Hello { public static void main(String[] args) { String s = "123"; System.out.println(s); m1(s); System.out.println(s); int i = 123; System.out.println(i); m2(i); System.out.println(i); } private static void m1(String s) { s = "321"; } private static void m2(int i) { i = 321; } } 
输出结果都是123。与汉字相关:java用的是Unicode 编码char 型变量的范围是0-65535 无符号的值,可以表示 65536个字符,基本上地球上的字符可被全部包括了,实际中,我们希望判断一个字符是不是汉字,或者一个字符串里的字符是否有汉字来满足业务上的需求,String类中有个这样的方法可得到其字符长度length() ,看下面例子,
String s1 = "我是中国人";
String s2 = "imchinese";
String s3 = "im中国人";
System.out.println(s1+":"+new String(s1).length());
System.out.println(s2+":"+new String(s2).length());
System.out.println(s3+":"+new String(s3).length());
OUTPUT:
我是中国人:5
imchinese:9
im 中国人:5
看到了吧,字符串里如果有双字节的字符java就把每个字符都按双字节编码,如果都是单字节的字符就按单字节编码
于是按照以上 的规律,结合一位QQ昵称 ?G茶?I珠海兄的提示由以下解决方法,就是判断字符串的长度和字符字节的长度是否相同来判断是否有双字节的字符
System.out.println((s1.getBytes().length == s1.length())?"s1无汉字":"s1有汉字");
System.out.println((s2.getBytes().length == s2.length())?"s2无汉字":"s2有汉字");
System.out.println((s3.getBytes().length == s3.length())?"s3无汉字":"s3有汉字");
OUTPUT:
s1有汉字
s2无汉字
s3有汉字 //
且慢,这样诚然可以判断出来一个串中是否有双字节编码的字符,但是要精确判断是否有汉字就有些麻烦了,我们知道还有许多其他国家的字符在Unicode中是双字节的.
于是,需要进一步确定汉字的编码范围怎么确定呢,我用了一个本办法那就是现在记事本输出0-65535之间的字符,通过观察发现第一个汉字是'一'最后一个是'??'(现在我也不认识);这下好了判断汉字就容易多了比如我们可以通过比较字符的编码范围,最后给大家一些我试验的结果汉字基本集中在[19968,40869]之间,共有20901个汉字(是不是少了点, 算算你能认识多少) .

split

public String[] split(String regex)
Splits this string around matches of the given regular expression.

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

The string "boo:and:foo", for example, yields the following results with these expressions:

RegexResult:{ "boo", "and", "foo" }o{ "b", "", ":and:f" }
Parameters:
regex - the delimiting regular expression
Returns:
the array of strings computed by splitting this string around matches of the given regular expression
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
Since:
1.4
See Also:
Pattern

spit接受的String参数应该是正则表达式.所以如果该参数本身是正则表达式的特殊字符 如 "|",使用是则需要用转义字符转义一下:
如:

String o=""兰州|广州|北京"

String[] rArray=o.split("\\|");

结果:

{ "兰州", "广州", "北京" }

substring()
它有两种形式,第一种是:String substring(int startIndex)
第二种 是:String substring(int startIndex,int endIndex)

concat() 连接两个字符串

replace() 替换
它有两种形式,第一种形式用一个字符在调用字符串中所有出现某个字符的地方进行替换,形式如下:
String replace(char original,char replacement)
例如:String s=”Hello”.replace(’l',’w');
第二种形式是用一个字符序列替换另一个字符序列,形式如下:
String replace(CharSequence original,CharSequence replacement)

trim() 去掉起始和结尾的空格

valueOf() 转换为字符串

toLowerCase() 转换为小写

toUpperCase() 转换为大写

length() 取得字符串的长度
例:
char chars[]={’a',’b’.’c'};
String s=new String(chars);
int len=s.length();

charAt() 截取一个字符
例:
char ch;
ch=”abc”.charAt(1);
返回值为’b’

getChars() 截取多个字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart 指定了子串开始字符的下标
sourceEnd 指定了子串结束后的下一个字符的下标。因此,子串包含从sourceStart到sourceEnd-1的字符。
target 指定接收字符的数组
targetStart target中开始复制子串的下标值
例:
String s=”this is a demo of the getChars method.”;
char buf[]=new char[20];
s.getChars(10,14,buf,0);

getBytes()
替 代getChars()的一种方法是将字符存储在字节数组中,该方法即getBytes()
例:
String s = “Hello!你好!”;
byte[] bytes = s.getBytes();

toCharArray()
例:
String s = “Hello!你好!”;
char[] ss = s.toCharArray();

equals() 和equalsIgnoreCase() 比较两个字符串

regionMatches() 用于比较一个字符串中特定区域与另一特定区域,它有一个重载的形式允许在比较中忽略大小写。
boolean regionMatches(int startIndex,String str2,int
str2StartIndex,int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex,String
str2,int str2StartIndex,int numChars)

startsWith() 和endsWith()
startsWith()方法决定是否以特定字符串开始,endWith()方法决定是否以特定字符串结束

equals() 和==
equals()方法比较字符串对象中的字符,==运算符比较两个对象是否引用同一实例。
例:String s1=”Hello”;
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false

compareTo() 和compareToIgnoreCase() 比较字符串

indexOf() 和lastIndexOf()
indexOf() 查找字符或者子串第一次出现的地方。
lastIndexOf() 查找字符或者子串是后一次出现的地方。

StringBuffer 构造函数
StringBuffer定义了三个构造函数:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)

下面是StringBuffer相关的函数:
length() 和capacity()
一个StringBuffer当前长度可通过length()方法得到,而整个可分配空间通过capacity()方法得 到。

ensureCapacity() 设置缓冲区的大小
void ensureCapacity(int capacity)

setLength() 设置缓冲区的长度
void setLength(int len)

charAt() 和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)

getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)

append() 可把任何类型数据的字符串表示连接到调用的StringBuffer对象的末尾。
例:int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append(”a=”).append(a).append(”!”).toString();

insert() 插入字符串
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
index 指定将字符串插入到StringBuffer对象中的位置的下标。

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

delete() 和deleteCharAt() 删除字符
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)

replace() 替换
StringBuffer replace(int startIndex,int endIndex,String str)

substring() 截取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)