顯示具有 Java SE::字串處理 標籤的文章。 顯示所有文章
顯示具有 Java SE::字串處理 標籤的文章。 顯示所有文章

2015年10月2日 星期五

正規化表示式

Pattern & Matcher 的應用:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegxDemo {
    public static void main(String[] args){
    
        String test = "He was a teacher";
        
        Pattern p1 = Pattern.compile("w.s");
        Matcher m1 = p1.matcher(test);
        if (m1.find()){
            System.out.println("Found: " + m1.group());
        }
        
        Pattern p2 = Pattern.compile("w[abc]s");
        Matcher m2 = p2.matcher(test);
        if (m2.find()){
            System.out.println("Found: " + m2.group());
        }
        
        Pattern p3 = Pattern.compile("t[^xyz]acher");
        Matcher m3 = p3.matcher(test);
        if (m3.find()){
            System.out.println("Found: " + m3.group());
        }

        String header = "<h1>Hello World<h1>";
        Pattern p4 = Pattern.compile("h1");
        Matcher m4 = p4.matcher(header);
        if (m4.find()){
            System.out.println("Found: " + m4.group());
            header = m4.replaceAll("p");
            System.out.println(header);
        }  

    }
}

Scanner 切割字串的應用


import java.util.Scanner;

public class ScannerDemo {
    public static void main(String[] args){
        StringBuilder sb = new StringBuilder(64);
        String test = "1.1, 2.2, 3.3";
        float fsum = 0.0f;
        
        Scanner s = new Scanner(test).useDelimiter(", ");
        while(s.hasNextFloat()){
            float f = s.nextFloat();
            fsum += f;
            sb.append(f).append(" ");
        }
        System.out.println("Values for: " + sb.toString());
        System.out.println("FSum: " + fsum);
    }
}

StringBuilder 用法


import java.io.PrintWriter;
import java.util.StringTokenizer;

public class StringDemo {
    public static void main(String[] args){
        
        //StringBuilder 範例
        StringBuilder sb = new StringBuilder(500);
        
        sb.append(", this is a good day!");
        sb.insert(0,"Today is Friday!");
        for (int i = 1; i< 11 ; i++){
            sb.append(i).append("    ");
        }
        sb.append("] times");
        System.out.println(sb.toString());
        System.out.println();
        
        //String Methods 範例
        PrintWriter pw = new PrintWriter(System.out, true);
        String tc01 = "It was the best of times";
        String tc02 = "It was the worst of times";
        
        if (tc01.equals(tc02)){
            pw.println("Strings match...");
        }
        if (tc01.contains("It was")){
            pw.println("It was found");
        }
        String temp = tc02.replace("w", "zw");
        pw.println(temp);
        pw.println(tc02.substring(5, 12));
         pw.println();
         
        //String Split 範例
        String shirts = "Blue Shirt, Red Shirt, Black Shirt, Maroon Shirt";
        
        String[] results = shirts.split(", ");
        for (String shirtStr:results){
            pw.println(shirtStr);
        }
         pw.println();
         
        //Tokenizer 範例
        StringTokenizer st = new StringTokenizer(shirts, ", ");
        while(st.hasMoreTokens()){
            pw.println(st.nextToken());
        }
    }
}

Properties 的使用


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class Demo1 {
    public static void main(String[] args) throws FileNotFoundException{
    
        Properties myProps = new Properties();
        try{
            FileInputStream fis = new FileInputStream("C:\\ServerInfo.properties");
            myProps.load(fis);
        } catch (IOException e){
            System.out.println("Error: " + e.getMessage());
        }
        
        System.out.println("Server: " + myProps.getProperty("hostName"));
        System.out.println("User: " + myProps.getProperty("userName"));
        System.out.println("Password: " + myProps.getProperty("password"));
           
    }
}

Properties 的檔案內容:
hostName=www.example.com
userName=user
password=pass

2015年9月11日 星期五

Java 的 String 物件

String 是常見的 Java 字串物件:
public class StringOperations {
    
    public static void main(String[] args) {
       
        String string2 = "World";
        String string3 = "";
        
        string3 = "Hello".concat(string2);
        System.out.println("String3: " + string3);
        
        System.out.println("Length: " + string3.length());
        
        System.out.println("Sub: " + string3.substring(0, 5));
        
        System.out.println("Upper: " + string3.toUpperCase());
    }
}