文章会有【原创】或者【转载】标示,部分来自Google Baidu的学习结果 {Java/PHP/Python/Ruby/Go}

Java 在学习二十六【原创】  


Java-Core 常用类

1String/StringBuffer/StringBuilder

2:正则表达式

3Math

4Date

5Calendar

6System


java.lang.String final 修饰,不能被其他类继承;

String的构造,也可以直接赋值

通过构造函数new的一定会产生空间,通过直接赋值不一定产生空间;其实是一种设计模式,享元模式;

String s6 = "a" + "b"; //jvm会自动优化

String s7 = "ab";

s6 == s7 true


package coreJava;


public class StringTest1 {


public static void main(String[] args) {

// TODO Auto-generated method stub

char[] chars = {'h', 'e', 'l', 'l', 'o'};

String s = new String(chars);

System.out.println(s);

String s1 = new String(chars, 1, 2); //从下标为1开始两个字符

System.out.println(s1);

String s2 = new String("hello");

String s5 = new String("hello");

System.out.println(s2);

String s3 = "hello";

System.out.println(s3); //直接赋值称为字符串常量,不是new对象

String s4 = "hello"; //字符串池的概念,里面存放字符串常量,每次先从池中查找;

System.out.println(s3 == s4);

System.out.println("##########################");

System.out.println(s2 == s5);

String s6 = "a" + "b"; //jvm会自动优化

String s7 = "ab";

System.out.println("##########################");

System.out.println(s6 == s7);

System.out.println("##########################");

System.out.println(s3.charAt(1)); //length - 1

System.out.println(s3.length()); //长度

System.out.println("##########################");

System.out.println("hello@imop.us".endsWith("imop.us"));

System.out.println("hello@imop.us".substring(1, 4)); //包含1不包含4

System.out.println("hello@imop.us".indexOf("@")); //子串存在的位置

System.out.println("##########################");

int sl = "hello_hhy@imop.us".indexOf("@");

System.out.println("hello_hhy@imop.us".substring(0, sl));  //截取邮箱前缀

//toUpperCase toLowerCase

System.out.println("hhy".equals("hhy")); //字符串内容是否相同

System.out.println("hhy".equalsIgnoreCase("Hhy")); //字符串内容是否相同

int a = 100;

System.out.println(String.valueOf(a));

String aa = "a,b,c";

String[] aas = aa.split(",");

for (String sa: aas) {

System.out.println("----" + sa);

}

StringBuffer sb1 = new StringBuffer();

sb1.append("aaabbb");

sb1.append("cccddd");

System.out.println(sb1);

StringBuilder sb2 = new StringBuilder();

sb2.append("aaabbb");

sb2.append("cccddd");

System.out.println(sb2);

System.out.println("###########################");

System.out.println(getStringLength("ab"));

}

public static String getStringLength(String s) {

int len = s.length();

StringBuilder sb = new StringBuilder();

sb.append("(");

for (int i = 0; i < len; i++) {

sb = i == 0 ? sb.append("?") : sb.append(",?");

}

sb.append(")");

return sb.toString();

}


}


正则表达式:

java.util.Patten

java.util.Matcher

package coreJava;


import java.util.Arrays;

import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class PattenTest {


public static void main(String[] args) {

// TODO Auto-generated method stub

String s = "xxab+dddddab+xxxffgab+";

String[] ss = s.split("ab\\+");

System.out.println(Arrays.toString(ss));

System.out.println("#############################");

Pattern p = Pattern.compile("(\\w+)(ab\\+)"); //静态方法

Matcher m = p.matcher(s);

//判断一个字符串是否匹配一个正则表达式

System.out.println(m.matches());

//在一个字符串中找出匹配正则表达式的元素

System.out.println("#############################");

m.reset(); //重置匹配位置

while(m.find())

    System.out.println(m.group());

    System.out.println(m.group(1));

    System.out.print("start:"+m.start()); 

    System.out.println("end:"+m.end()); 

}


}