- //熟悉String类的常用方法
- class StringMethodDemo
- {
- public static void method_get(String str)//获取
- {
- sop(str.charAt(4));
- sop(str.indexOf('a'));
- sop(str.indexOf('a',4));
- sop(str.indexOf("ac",8));
- sop(str.length());
- sop(str.lastIndexOf('a'));//反向索引
- }
- public static void method_judge(String str)//判断:通常用来判断文件名
- {
- sop(str.contains("ac"));
- sop(str.isEmpty());
- sop(str.startsWith("ac"));
- sop(str.endsWith("ce"));
- sop(str.substring(3));
- sop(str.substring(3,8));
- }
- public static void method_trans(String str)//转换,
- { sop(str.trim());
- sop(str.toUpperCase());
- sop(str.toLowerCase)
- char[] c=str.toCharArray();
-
- for (int x=0;x<c.length ;x++ )
- {
- sop(c[x]);
-
-
- }
- String s =new String(c);
- sop("s="+s);
- sop(str.replace('a','b'));
- String[] b=str.split(" ");
- for (int x=0;x<b.length ;x++ )
- {
- sop(b[x]);
- }
- }
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- public static void main(String[] args)
- {
- String str=" ac ac ac acbcece ";
- method_get(str);
- method_judge(str);
- method_trans(str);
- }
- }
复制代码 |
|