免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 865 | 回复: 0
打印 上一主题 下一主题

JAVA―String 类小结 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-21 11:35 |只看该作者 |倒序浏览

JAVA―String 类小结
1.String (char a[]) 用一个字符数组a 创建一个字符串对象,如
  char a[]={‘b’,’o’,’y’};
  String s=new String(a);
  上述过程相当于 String s= "boy";
2. String(char a[],int startIndex,int count) 提取字符数组a 中的一部分字符创建一个字符串对象 ,参数startIndex 和count 分别指定在a 中提取字符的起始位置和从该位置开始截取的字符个数,例如
char a[]={‘s’,’t’,’b’,’u’,’s’,’n’};
String s=new String(a,2,3);
上述过程相当于 String s= "bus";
3 equals方法 equalsIgnoreCase方法忽略大小写
字符串对象调用String类中的public boolean equals(String s)方法比较当前字符串对象的实体是否与参数指定的字符串s的实体相同.如
String tom=new String( "we are students");
String boy=new String( "We are students");
String jerry= new String("we are students");
tom.equals(boy)的值是false,
tom.equals(jerry)的值是 true.
4 startsWith,endsWith方法
String tom= "220302620629021",jerry= "21079670924022";
tom.startsWith("220")的值是true
jerry.startsWith("220")的值是false.
5 regionMatches方法
字符串调用
public boolean regionMatches(int firstStart,String other,int ortherStart,int length)
方法,从当前字符串参数firstStart指定的位置开始处,取长度为length的一个子串,并将这个子串和参数other 指定的一个子串进行比较,其中,other 指定的子串是从参数othertStart 指定的位置开始,从other中取长度为length的一个子串.如果两个子串相同该方法就返回true,否则返回false.
使用该方法的重载方法
public boolean regionMatches(boolean b,int firstStart,String other,int ortherStart,intlength)
可以通过参数b决定是否忽略大小写,当b取true时,忽略大小写.
6 compareTo,compareToIgnoreCase方法
字符串对象可以使用String类中的
public int compareTo(String s)方法,按辞典序与参数s指定的字符串比较大小.
如果当前字符串与s相同,该方法返回值0
如果当前字符串对象大于s,该方法返回正值
如果小于s,该方法返回负值.
7 public int indexOf (String s) 字符串调用该方法从当前字符串的头开始检索字符串s,并返回首次出现s的位置.如果没有检索到字符串s,该方法返回的值是-1.
public int indexOf(String s ,int startpoint) 字符串调用该方法从当前字符串的startpoint 位置处开始检索字符串s,并返回首次出现s 的位置.如果没有检索到字符串s,该方法返回的值是-1.
public int lastIndexOf (String s) 字符串调用该方法从当前字符串的头开始检索字符串s,并返回最后出现s的位置.如果没有检索到字符串s,该方法返回的值是-1.
public int lastIndexOf(String s ,int startpoint) 字符串调用该方法从当前字符串的startpoint 位置处开始检索字符串s,并返回最后出现s 的位置.如果没有检索到字符串s,该方法返回的值是-1.
8 public String substring(int startpoint) 字符串对象调用该方法获得一个当前字符串的子串,该子串是从当前字符串的startpoint处截取到最后所得到的字符串.
public String substring(int start ,int end) 字符串对象调用该方法获得一个当前字符串的子串,该子串是从当前字符串的start 处截取到end 处所得到的字符串,但不包括end处所对应的字符.
9 public String replace(char oldChar,char newChar) 字符串对象s调用该方法可以获得一个串对象,这个串对象是用参数newChar 指定的字符替换s 中由oldChar 指定的所有字符而得到的字符串.
public String replaceAll(String old ,String new) 字符串对象s调用该方法可以获得一个串对象,这个串对象是通过用参数new指定的字符串替换s 中由old 指定的所有字符串而得到的字符串.
Public String trim() 一个字符串s 通过调用方法trim()得到一个字符串对象,该字符串对象是s去掉前后空格后的字符串.
10 转化为整型
java.lang包中的Integer类调用其类方法
public static int parseInt(String s)
可以将 “数字” 格式的字符串,如"12387",转化为int型数据.例如
int x;
String s="6542";
x=Integer.parseInt("6542");
类似地,使用java.lang包中的Byte,Short,Long类调相应的类方法
public static byte parseByte(String s)
public static short parseShort(String s)
public static long parseLong(String s)
可以将“数字”格式的字符串,转化为相应的基本数据类型
11 转化为float型或double型java.lang包中的Float类调用其类方法
  public static int parseFloat (String s)可以将 “数字”格式的字符串,如"12387.8976",转 化为float 型数据.例如
  float n=Float.parseFloat("12387.8976")

  String s= new String(“12387.8976”);
  float n=Float.parseFloat(s)
12 public StringvalueOf( byte n)
  public StringvalueOf (int n)
  public StringvalueOf (long n)
  public StringvalueOf (float n)
  public StringvalueOf (double n)
将形如123,1232.98等数值转化为字符串,如
String str=String.valueOf(12313.9876);
float x=123.987f;
String temp=String.valueOf(x);
13 将字符串中的字符拷贝到字符数组
public void getChars(int start,int end,char c[],int offset ) 字符串调用getChars 方法将当前字符串中的一部分字符拷贝到参数c 指定的数组中,将字符串中从位置start 到end-1位置上的字符拷贝的数组c中,并从数组c 的offset处开始存放这些字符.需要注意的是,必须保证数组c能容纳下要被拷贝的字符.


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/47207/showart_427424.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP