java中String类的用法

1.String  

String类很常用,很重要。 
 

String不像int或float, 它是参考类型。final类型, 不能被继承,String is a Reference Type,Defined in java.lang package

常用方法:
length()
String greeting = “Hello”;
int n = greeting.length();//is 5
charAt(n)(取某个位置字符)
char first = greeting.charAt(0);
char last = greeting.charAt(4);
substring()(取子字符串)
String s = greeting.substring(0,3);//from 0 inclusive to 3 exclusive
Concatenation(链接)
String a = greeting + “ world!”+ 2009;
Equality(don’t use ==)(测试是否相等)
String s = “Hello”; s.equals(greeting);
“Hello”.equalsIgnoreCase(“hello”);(忽略大小写的测试相等)

本章源码
例子:
public class Test {
    public static void main(String args[]) {
        String letters = "abcdefghijklabcdefghijkl";
/*这里讲讲阅读源代码,control点击进入方法*/
        System.out.println("'c'在第" + letters.indexOf('c') + "个");
/* indexOf(int ch, int fromIndex) Returns the index within this string
of the first occurrence of the specified character, starting the

版权保护,原文出处:http://www.mark-to-win.com/JavaBeginner/JavaBeginner2_web.html#String

猜你喜欢

转载自blog.csdn.net/mark_to_win/article/details/89243708