字符串的一些知识

来源:互联网 发布:博物馆软件 编辑:程序博客网 时间:2024/06/11 23:30

1.String转换成整数

String a = "123";

Integer.parseInt(a);

throws NumberFormatException

Integer.valueOf(a);

调用parseInt() 抛出异常同上

Integer.valueOf('8') = 56 而不是预想的8

可以用'8'-'0'

2.日期型String格式转换

         dateFormat = new SimpleDateFormat("yyyy/MM/dd");
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01        dateFormat.setLenient(false);
        try        {             dateFormat.parse(s);//是要判断的字符串 返回date类型的数据即为转换之后的    Date 格式 可以 getYear getMonth getDay             return true;         }        catch (Exception e)        {            // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对            return false;        }



3.正则表达式的使用(以下内容 均摘自 http://blog.csdn.net/kdnuggets/article/details/2526588)

   //查找以Java开头,任意结尾的字符串
  Pattern pattern = Pattern.compile("^Java.*");
  Matcher matcher = pattern.matcher("Java不是人");
  boolean b= matcher.matches();
  //当条件满足时,将返回true,否则返回false
  System.out.println(b);


◆以多条件分割字符串时
Pattern pattern = Pattern.compile("[, |]+");
String[] strs = pattern.split("Java Hello World  Java,Hello,,World|Sun");
for (int i=0;i<strs.length;i++) {
    System.out.println(strs[i]);
}

◆文字替换(首次出现字符)
Pattern pattern = Pattern.compile("正则表达式");
Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");
//替换第一个符合正则的数据
System.out.println(matcher.replaceFirst("Java"));

◆文字替换(全部)
Pattern pattern = Pattern.compile("正则表达式");
Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");
//替换第一个符合正则的数据
System.out.println(matcher.replaceAll("Java"));


◆文字替换(置换字符)
Pattern pattern = Pattern.compile("正则表达式");
Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World ");
StringBuffer sbr = new StringBuffer();
while (matcher.find()) {
    matcher.appendReplacement(sbr, "Java");
}
matcher.appendTail(sbr);
System.out.println(sbr.toString());

◆验证是否为邮箱地址

String str="ceponline@yahoo.com.cn";
Pattern pattern = Pattern.compile("[//w//.//-]+@([//w//-]+//.)+[//w//-]+",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.matches());

◆去除html标记
Pattern pattern = Pattern.compile("<.+?>", Pattern.DOTALL);
Matcher matcher = pattern.matcher("<a href=/"index.html/">主页</a>");
String string = matcher.replaceAll("");
System.out.println(string);

◆查找html中对应条件字符串
Pattern pattern = Pattern.compile("href=/"(.+?)/"");
Matcher matcher = pattern.matcher("<a href=/"index.html/">主页</a>");
if(matcher.find())
  System.out.println(matcher.group(1));
}

◆截取http://地址
//截取url
Pattern pattern = Pattern.compile("(http://|https://){1}[//w//.//-/:]+");
Matcher matcher = pattern.matcher("dsdsds<http://dsds//gfgffdfd>fdf");
StringBuffer buffer = new StringBuffer();
while(matcher.find()){             
    buffer.append(matcher.group());       
    buffer.append("/r/n");             
System.out.println(buffer.toString());
}
       
◆替换指定{}中文字

String str = "Java目前的发展史是由{0}年-{1}年";
String[][] object={new String[]{"//{0//}","1995"},new String[]{"//{1//}","2007"}};
System.out.println(replace(str,object));

public static String replace(final String sourceString,Object[] object) {
            String temp=sourceString;   
            for(int i=0;i<object.length;i++){
                      String[] result=(String[])object[i];
               Pattern    pattern = Pattern.compile(result[0]);
               Matcher matcher = pattern.matcher(temp);
               temp=matcher.replaceAll(result[1]);
            }
            return temp;
}

4.正则表达式实现及测试

    public static int getvalue(String s)
    {
        int result = 0;
            Pattern pattern = Pattern.compile("\\d*-[0-1]?\\d-(([0-2]?\\d)|(3[0-1]))");
        Matcher matcher = pattern.matcher(s);
        System.out.println(matcher.matches());
        return result;
    }
    public static void main(String a[])
    {
        getvalue("1991-12-31");
    }


0 0