关于String类中常用的方法

在Java中,常用处理字符串的类,在java.lang包中,分别是String和StringBuffer,这两个类被声明为final,所以它们没有子类,不能自定义类来继承它们。 因为String和StringBuffer类在java.lang包中,所以它们可以自动被所有程序,即使用时不用import来引入它们即可。
这里就总结一下String类的常用方法
1、public int length()方法
字符串的长度:是指字符串所包含的字符的个数,调用String类的length() 方法可以得到一个字符串的长度。如:“你好abcd”.length()的值为6
2、字符串连接
“+”运算符可以连接两个字符串,产生一个String对象。也允许使用一连串的“+”运算符,把多个字符串对象连接成一个字符串对象。
字符串还可以和其他基本类型的数据连接,连接后成为新的字符串。如 int age=10; String str=“He is”+age+”years old”;
3、public char charAt(int index)
返回字符串指定位置上的字符。索引范围是从0到length() – 1;其中,index是想要得到的字符的下标,并且其值必须为非负的。

例如:String str=“hello world”;
           char ch=str.charAt(1);   //e

4、public void getChars(int Start,int End,char[] target,int targetStart)可以从字符串中截取多个字符。
其中,Start是开始位置,End是结束位置,因此截取的字符串包含了从Start到End的字符(不包括End位置上字符),截取的字符串存放在字符数组target中,从targetStart位置开始存,在此必须确保target足够大,能容纳所截取的字符串。否则,会抛出数组越界异常。
5、public byte[] getBytes()方法
使用操作系统平台的默认字符集将此字符串编码为 byte 序列,并将结果存储到一个byte 数组中。例如:中文Xp系统默认gbk字符集(有1个字节字符编码(和ASCII码一致),也有2个字节的字符编码(比如汉字、特殊字符等))

例如:String str=“abc”;
          byte[] bytes=str.getBytes();          System.out.println(Arrays.toString(bytes));//[97,98,99]
public byte[] getBytes(String charsetName);

使用指定的字符集将此字符串编码为 byte 序列,并将结果存储到一个byte 数组中。

例如:String str=“中国”; byte[] bytes=str.getBytes(“utf-8”);
      String  new_str=new String(bytes,”utf-8”);
      System.out.println(new_str);  //中国

注意:在使用getBytes()方法对字符串编码时,特别是中文字符串,使用不同的字符集,导致编码结果是不同的。
7、 字符串的比较
(1) public boolean equals(Object obj)和public boolean equalsIgnoreCase(String anotherString)方法
这两个方法可以比较两个字符串内容是否相等。如果两个字符串具有相同的字符和长度,它返回true;否则返回false。前者比较两个字符串的时候对字母大小写是敏感的,而后者不区分字母大小写。
(2) public boolean startsWith(String s)和public boolean endsWith(String s)方法
startsWith()方法判断当前字符串的前缀是否是字符串s,而endsWith()方法判断当前字符串的后缀是否是字符串s。

例如:String tom=“2605111”; String jerry=“2105222”
  tom.startsWith(“260”)的值是true;jerry.startWith(“260”)的值是false
tom.endsWith(“111”)的值是true,jerry.endsWith(“111”)的值是false

3) equals()与= =的区别
equals()方法比较两个字符串对象中的内容是否相等,(比较内容)而= =运算符是比较两个字符串对象是否指向同一对象(比较地址)
(4) public int compareTo(String s)
比较两个字符串的大小,s是被比较的字符串,此方法就是依次比较两个字符串对应字符的unicode值。如果两个字符的值相等则继续后续比较,否则直接返回两个unicode的差值。如果两个字符串完全一样,则返回0。
a.compareTo(s):值为负值时,表示a小于s;
值为正值时,表示a大于s;
值等于0时,表示a等于s;
(5) public boolean contains(String s)方法
判断当前字符串是否含有字符串s,如果含有返回true;否则,返回false。

例如:String tom=“student”; tom.contains(“stu”); //true    tom.contanins(“ok”);//false

8、 字符串的搜索
允许在字符串中搜索指定的字符或子字符串。其中,indexOf方法用来搜索字符或子字符串首次出现的位置,而lastIndexOf方法用来搜索字符或子字符串最后一次出现的位置。
(1) indexOf()方法
indexOf方法有4种形式,分别如下:

public int indexOf(int ch)//”abca”.indexOf(97);返回97对应的字符a,在字符串中首次出现的位置;结果为0
public int indexOf(int ch,int fromIndex)//”abca”.indexOf(97,1);返回97对应的字符a,从指定的索引位置1开始,在字符串中首次出现的位置;结果为3;
public int indexOf(String str)//”abca”.indexOf(“abc”);返回指定的子字符串abc在字符串adca中首次出现的位置;结果为0;
public int indexOf(String str,int fromlndex)//“abca”.indexOf(“ca”,1);返回指定的字符串ca,从指定的索引位置1开始,在字符串中首次出现的位置;结果为2

注意:未搜索到字符或字符串则返回-1
(2) lastlndexOf()方法
lastlndexOf方法也有4种形式,分别如下。

int lastIndexOf(int ch)//”abca”.lastIndexOf(97);  结果3
int lastIndexOf(int ch,int fromIndex)//”abca”.lastIndexOf(97,4);结果-1
int lastIndexOf(String str)//”abca”.lastIndexOf(“abc”);结果0
int lastlndexOf(String str,int fromlndex) //”abca”.lastIndexOf(“ca”,1);结果2

9、字符串修改
字符串的修改包括取字符串中的子串、字符串之间的连接、替换字符串中的某字符、消除字符串的空格等功能。在String类中有相应的方法来提供这些功能:

String substring(int startIndex)//返回从索引位置startIndex开始到字符串结尾的子字符串(新产生的字符串),原字符串保持不变。
如:“abca”.substring(1); //”bca”
String substring(int startIndex,int endlndex)//返回从索引位置startIndex开始到索引位置endIndex结束的子字符串,不包括endIndex位置上字符。
如:”abca”.substring(1,3); //”bc”
String concat(String str)//用来连接两个字符串,创建一个新的字符串对象,该对象包含原字符串,同时把str字符串跟在原字符串后面。原字符串保持不变。

如:“abca”.concat(“123”);//”abca123”
String replace(char original,char replacement)//用来替换到字符串中的某个字符,会创建一个新的字符串对象,原字符串保持不变。
如:”abca”.replace(‘a’,’m’);  //”mbcm”

String trim()//用来去除字符串前后多余的空格

如:”..”+” abca ”+”..”;   //”.. abca ..”
“..”+” abca ”.trim()+”..” //”..abca..”

注意:因为String字符串是不能改变的对象,因此调用上述方法对字符串进行修改都会产生新的字符串对象,原字符串保持不变。
10、 valueOf()方法
valueOf()方法是String类的静态方法,利用这个方法,可以将几乎所有的Java基本数据类型转换为String类型。这个方法是String类型和其他Java基本类型之间的一座转换桥梁。除了把Java中的基本类型转换为字符串之外,valueOf方法还可以把Object类和字符数组转换为字符串。
valueOf()的通用形式如下,总共有9种形式。

static String valueOf(boolean b)
static String valueOf(char c)
static String valueOf(char[] data)
static String valueOf(char[] data,int offset,int count)
如:char[] chars={‘w’,’e’,’l’,’c’,’o’,’m’};
String.valueOf(chars,3,3);   //”com”
static String valueOf(double d)
static String valueOf(float f)
static String valueOf(int i)
static String valueOf(long l)
static String valueOf(Object obj)
11、 public static int parseInt(String s)方法

是java.lang包中的Integer类的静态方法,可以将由“数字”字符组成的字符串,如“123456”,转化为int型数据,例如:
int x=Integer.parInt(“123456”); //123456
类似地,使用java.lang包中的Byte,Short,Double,Long,Float类的静态方法;

public static byte parseByte(String s);
public static short parseShort(String s);
public static long parseLong(String s);
public static float parseFloat(String s);
public static double parseDouble(String s);

将有“数字”字符组成的字符串,转化成基本数据类型。
还可以使用Long类中的下列静态方法得到整数的各种进制的字符串表示:

public static String toBinaryString(long i);
public static String toOctalString(long i);
public static String toHexString(long i);
public static String toString(long i,int p); //返回整数i的p进制表示
Long.toString(8,2);  //”1000”

12、public String toString()方法
所有的类都默认是java.lang包中Object类的子类。Object类有一个public String toString()方法,一个对象通过调用该方法可以获的该对象的字符串表示。
一个对象调用toString()方法返回字符串的一般形式:
创建对象的类的名字@对象的引用的字符串表示

Student stu=new Student();
stu.toString();  //Student@158f9d3

显然默认的toString()方法是不能满足子类要求的,所以子类可以覆盖toString()方法。例如:java.util包中Date类就覆盖了toString方法,覆盖的方法返回时间的字符串表示。

/*【例5-6 】定义一个Person类,覆盖Object的toString()方法,再定义一个测试类,输出Person的信息。*/
class Person {
	String name;
	int age;
	Person(String n,int a){
		this.name=n;
		this.age=a;
	}
	public String toString(){   //覆盖超类的toString()方法,返回自己的字符串对象
		return "姓名是"+name+",年龄是"+age+"岁";
	}
	public String old(){
	return super.toString();
	}
}
public class PersonDemo{
	public static void main(String[] args) {
		Person p=new Person("王冷晗",30);
		System.out.println(p.toString());
		System.out.println(p.old());
	}

猜你喜欢

转载自blog.csdn.net/qq_43747311/article/details/84575210