SummerVocation_Learning--java的String类方法总结

壹:

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 equalsIngnoreCase(String another),比较字符串与another是否一样。忽略大小写。

public String replace(char oldChar, char newChar),在字符串中用newChar字符替换oldChar字符。

举例:

public class TestString {

	public static void main(String[] args) {
		String s1 = "sun java",s2 = "sun Java";
		System.out.println(s1.charAt(4)); //j
		System.out.println(s1.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 = "sun orcle";
		String str = s.replace('s', 'S');
		System.out.println(str); //Sun java
		
	}

}

贰:

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 trim(),返回该字符串去掉开头和结尾空格后的字符串。

举例:

public class TestString {

	public static void main(String[] args) {
		String s = "Welcome to Beijing!";
		String s1 = " sun java ";
		System.out.println(s.startsWith("Welcome"));//true
		System.out.println(s.endsWith("Beijing"));//false,少了‘!'
		String sL = s.toLowerCase();
		String sU = s.toUpperCase();
		System.out.println(sL+"\n"+sU);
		String subs = s.substring(11);
		System.out.println(subs); //Beijing!
		String st = s1.trim();
		System.out.println(st); //sun java
	}

}

叁:

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

        如public static String valueOf(double d)

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

举例:

public class TestString {

	public static void main(String[] args) {
		int n = 1234567;
		String sNumber = String.valueOf(n);
		System.out.println("n is the "+sNumber.length()+"-digit number");
		//n is the 7-digit number
		String s = "Mary,F,1992";
		String[] s2 = s.split(",");
		for (int i = 0; i < s2.length; i++) {
			System.out.println(s2[i]);
		}
		/*
		 * Mary
		 * F
		 * 1992
		 */
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值