accessstring.java

来自「java的书上例子」· Java 代码 · 共 25 行

JAVA
25
字号
//例7.2 String类的对象的访问
class accessString {
		public static void main(String argvs[]){
			String s = "This program will show you how to use the class String!";
			System.out.println("from string  \""+s+"\"");
			System.out.println("You can :\n1. get the length :"+s.length());
			System.out.println("2. know the char at the specified index 6 is :"+s.charAt(6));
			System.out.println("3. index a character or a substring in several ways as");
			System.out.println("	the first occurrence of  'a'is :"+s.indexOf('a'));
			System.out .println("	the first occurrence of  'a' from index 15 is :"+s.indexOf('a',15));
			System.out.println("	the first occurrence of  'ow'is :"+s.indexOf("ow"));
			System.out .println("	the first occurrence of  'ow' from index 15 is :"+s.indexOf("ow",25));
			System.out.println("	the last occurrence of  'a'is :"+s.lastIndexOf('a'));
			System.out .println("	the last occurrence of  'a' from index 15 is :"+s.lastIndexOf('a',15));
			char sub[] = new char[10];
			byte sub2[] = new byte[10];
			System.out.print("4.	get chars into an array from index 5 to 12:");
			s.getChars(5,12,sub,0);
		    System.out.println(sub);
			System.out.print("5.	get the ascii code of 'a':");
			System.out.println(sub2[5]);
		}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?