String类

一、字符串相关类(String、StringBuffer)

1.String类

a )  java.lang.String 类代表不可变的字符序列。

b ) "......"为该类的一个对象。

c ) String类的常见构造方法:

String(String original)

创建一个String对象为original的拷贝。

String(char[ ] value)

用一个字符数组创建一个String对象。

String(char[ ] value, int offset, int count)

用一个字符数组从offset项开始的count个字符创建一个String对象。

String类举例(1)

public class Test {
	String s1 = "hello";
	String s2 = "world";
	String s3 = "hello";
	System.out.println(s1 == s3); /*true   s1==s3比较的是s1与s3的引用。hello是一个字符串常量,内存中分布在data segment中。
								  s1与s3指向了同一个内容*/
	s1 = new String("hello");
	s2 = new String("hello");
	System.out.println(s1 == s2);     //false 虽然内容一样,但是指向了不同的对象
	System.out.println(s1.equals(s2));/*true   
					    equals的默认方法是就是看s1是不是等于s2。
					    但String类中重写了该方法,比较的是字符串是否相等*/
	char c[] = {'s','u','n','','j','a','v','a'};
	String s4 = new String(c);
	String s5 = new String(c,4,4);
	System.out.println(s4); //sun java
	System.out.println(s5); //java
}

String 类常用方法(1)

public char charAt ( int index )

返回字符串中的第index个字符


public int length()

返回字符串的长度

public int indexOf ( String str )

返回字符串中出现str的第一个位置


public int indexOf ( String str, int fromIndex )

返回字符串中从fromIndex开始出现str的第一个位置


public boolean equalsIgnoreCase ( String another )

比较字符串与another是否一样(忽略大小写)

public String replace ( char oldChar, char newChar )

在字符串中用newChar字符替换oldChar字符

public class Test {
	public static void main(String[] args) {
		String s1 = "sun java",s2 = "Sun Java";
		System.out.println(s1.charAt(1));//u
		System.out.println(s2.length());//8
		System.out.println(s1.indexOf("java"));//4
		System.out.println(s1.indexOf("Java"));//-1
		System.out.println(s1.equals(s2));//false
		System.out.println(s1.equalsIgnoreCase(s2));//true
		
		String s = "我是程序员,我在学Java";
		String sr = s.replace('我','你');
		System.out.println(sr);//你是程序员,你在学Java
	}


String 类常用方法 (2)

public boolean startsWith( String prefix )

判断字符串是否以prefix字符串开头

public boolean endsWith( String prefix)

判断字符串是否以prefix字符串结尾


public String toUpperCase( )

返回一个字符串为该字符串的大写形式


public String toLowerCase( )

返回一个字符串为该字符串的小写形式


public String substring( int beginIndex )

返回该字符串从beginIndex开始到结尾的子字符串


public String subString ( int beginIndex, int endIndex )

返回该字符串从beginIndex开始到endIndex结尾的子字符串


public String trim ( )

返回将该字符串去掉开头和结尾空格后的字符串

public class Test {
	public static void main(String[] args) {
		String s = "Welcome to Java World!";
		String s1 = "  sun java  ";
		System.out.println(s.startsWith("Welcome"));//true
		System.out.println(s.endsWith("world"));//false
		String sL = s.toLowerCase();
		String sU = s.toUpperCase();
		System.out.println(sL);//welcome to java world!
		System.out.println(sU);//WELCOME TO JAVA WORLD!
		String subS = s.subString(11);//Java World!
		System.out.println(subS);
		String sp = s1.trim();
		System.out.println(sp);//sun java
	}
}

String 类常用方法 (3)

静态重载方法

public static String valueOf ( ... ) 可以将基本类型数据转换为字符串。

例如:

public static String valueOf ( double d )

public static String valueOf ( int i )

方法public String[ ] split( String regex ) 可以将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组。

public class Test {
	public static void main(String[] args) {
		int j = 1234567;
		String sNumber = String.valueOf(j);
		System.out.println("j 是"+sNumber.length()+"位数。");
		String s = "Mary,F,1976";
		String[] sPlit = s.split(",");
		for(int i = 0; i<sPlit.length();i++) {
			System.out.println(sPlit[i]);
		}
	}
}
/*输出结果:
	j 是7位数
	Mary
	F
	1976
*/










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值