- package com.xlunaer.alan;
- public class StringTest {
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String s = " abc defg hijkl mnopq rstuvw xyz &&& hello java 1234567";
-
- //获取
- char s1 = s.charAt(1);
- int s2 = s.codePointAt(1);
- int length = s.length();
- int s3 = s.indexOf(98);
- int s4 =s.indexOf("mn", 0);
- int s5 = s.lastIndexOf("a");
- System.out.println(s1 + "\t" + s2 + "\t" + length + "\t" + s3 + "\t" + s4 + "\t" + s5);
-
- //判断
- out(s.contains("abc"));
- out(s.isEmpty());
- out(s.equals("abc defg hijkl mnopq rstuvw xyz &&& hello java 1234567"));
- out(s.contentEquals("abc defg hijkl mnopq rstuvw xyz &&& hello java 1234567"));
- out(s.endsWith("567"));
- out(s.startsWith("abc de"));
-
- //转换
- out(s.toUpperCase());
- out(s.toUpperCase().toLowerCase());
- out(new String(new char[] {'a', 'b', 'c'}));
- char[] c = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
- String s6 =new String(c, 1, 5);
- out(s6);
- out(String.valueOf(c));
-
- //替换
- out(s.replace('a', 'c'));
- out(s.replace("a", "java"));
-
- //分割
- String[] ss = s.split(" ");
- for(String v: ss) {
- out(v);
- }
-
- //子串
- out(s.subSequence(1, 3));
-
- //去除空格
- out(s.trim());
-
- //比较
- //相等返回 0 ; 在字典上比源字符串大返回一个整数; 反之返回一个负数
- out(s.compareToIgnoreCase(" ABC DEFG HIJKL MNOPQ RSTUVW XYZ &&& HELLO JAVA 1234567"));
-
- out(noneSpace(s));
- }
- public static void out(Object obj) {
- System.out.println(obj);
- }
-
- public static String noneSpace(String s) {
- char[] c2 = s.toCharArray();
- String s2 = "";
- for(char c: c2) {
- if(c != ' ') {
- s2 += c;
- }
- }
- return s2;
- }
- }
复制代码 |
|