public class StringTest { public static void main(String[] args){ String mystring = "HelloWorld"; int len = mystring.length(); System.out.println("字符串長度是:" + len); } }
輸出結果是10java
public class StringTest { public static void main(String[] args){ String mystring = "HelloWorld"; char s = mystring.charAt(3); System.out.println(s); } }
輸出結果是HelloWorld中的第四個字符lspa
public class StringTest { public static void main(String[] args){ String mystring = "HelloWorld"; String s = mystring.substring(0,5); System.out.println(s); } }
輸出爲字符串hello(注意索引前包後閉),還有一個重載方法參數只有一個String substring(int beginIndex):返回從第一個索引到最後的子字符串。code
public class StringTest { public static void main(String[] args){ String mystring = "HelloWorld"; String hellojava = "HelloJava"; boolean equal = mystring.equals(hellojava); System.out.println(equal); } }
返回falseblog