陳鍾誠的程式作品集 -- Java 字串函數

程式作品

C 語言

Java

C#

JavaScript

常用函數

文字處理

遊戲程式

衛星定位

系統程式

資料結構

網路程式

自然語言

人工智慧

機率統計

資訊安全

等待完成

訊息

相關網站

參考文獻

最新修改

簡體版

English

package ccc;
import java.util.*;
import java.io.*;
 
public class STR {
  public static void main(String[] args) throws Exception {
    text2file("This is a test \nof STR.text2file().", "STR\\STR.out");
    System.out.println(file2text("STR\\STR.out"));
    String xml = "<person name=\"ccc\">ccc@mail.km.kuas.edu.tw<tel>082313530</tel><tel>0938707315</tel></person>";
    System.out.println("xml = "+xml);
    System.out.println("replace(ccc,Chung-Chen Chen) = "+replace(xml, "ccc", "Chung-Chen Chen"));
    System.out.println("expand(xml) = "+expand(xml, "ccc=Chung-Chen Chen"));
    System.out.println("xml2text(xml) = "+xml2text(xml));
    System.out.println("innerText(xml, >, </) = "+innerText(xml, ">", "<"));
    System.out.println("tagValue(xml, name) = "+tagValue(xml, "name"));
    System.out.println("trim(..abc.., .) = "+trim("..abc..", '.'));
  }
 
  public static String head(String pStr, String pSpliter) {
    int spliterPos = pStr.indexOf(pSpliter);
    if (spliterPos < 0) return pStr;
    return pStr.substring(0,spliterPos);
  }
 
  public static String tail(String pStr, String pSpliter) {
    int spliterPos = pStr.indexOf(pSpliter);
    if (spliterPos < 0) return "";
    return pStr.substring(spliterPos+pSpliter.length());
  }
 
  public static String last(String pStr, String pSpliter) {
    int spliterPos = pStr.lastIndexOf(pSpliter);
    if (spliterPos < 0) return pStr;
    return pStr.substring(spliterPos+1);
  }
 
  public static String noLast(String pStr, String pSpliter) {
    int spliterPos = pStr.lastIndexOf(pSpliter);
    if (spliterPos < 0) return pStr;
    return pStr.substring(0, spliterPos);
  }
 
  public static String noNull(String pStr) { 
      if (pStr == null) return ""; return pStr; 
  }
 
  public static String file2text(String inFile, String pEncode) throws Exception {
    FileInputStream fis = new FileInputStream(inFile);
    InputStreamReader isr = new InputStreamReader(fis);
    if (pEncode != null) isr = new InputStreamReader(fis, pEncode);
    BufferedReader reader=new BufferedReader(isr, 4096);
    StringBuffer rzText = new StringBuffer(); 
    do {
        String line = reader.readLine();
        if (line == null) break;
        rzText.append(line+"\n");
    } while (true);
    reader.close();
    return rzText.toString();
  }
 
  public static String file2text(String inFile) throws Exception {
      return file2text(inFile, null);
  }
 
  public static void text2file(String pText, String outFile, String pEncode) throws Exception {
    FileOutputStream fos = new FileOutputStream(outFile);
    Writer writer;
    if (pEncode == null) 
        writer =new OutputStreamWriter(fos);
    else
        writer = new OutputStreamWriter(fos, pEncode);
    writer.write(replace(pText, "\n", "\r\n"));
    writer.close();
  }
 
  public static void text2file(String pText, String outFile) throws Exception {
      text2file(pText, outFile, null);
  }
 
  public static String replace(String pStr, String fromPat, String toPat) {
    if (fromPat.length()==0) return pStr;
    if (pStr.indexOf(fromPat)<0) return pStr;
    StringBuffer rzStr = new StringBuffer();
    int strIdx = 0, nextIdx;
    while ((nextIdx = pStr.indexOf(fromPat, strIdx))>=0) {
      rzStr.append(pStr.substring(strIdx, nextIdx));
      rzStr.append(toPat);
      strIdx = nextIdx + fromPat.length();
    }
    rzStr.append(pStr.substring(strIdx));
    return rzStr.toString();
  }
 
  public static String innerText(String pXml, String beginMark, String endMark) {
    int beginStart = pXml.indexOf(beginMark);
    if (beginStart < 0) return null;
    int beginEnd = beginStart+beginMark.length();
    int endStart = pXml.indexOf(endMark, beginEnd);
    if (endStart < 0) return null;
    return pXml.substring(beginEnd, endStart);
  }
 
  public static String tagValue(String pXml, String pTag) {
    String rzValue=innerText(" "+pXml, " "+pTag+"=\"", "\"");
    if (rzValue.length()==0) return innerText(" "+pXml, ":"+pTag+"=\"", "\"");
    return rzValue;
  }
 
  public static String expand(String pText, String pMacros) {
    String[] macros = pMacros.split("\\|");
    for (int i=0; i<macros.length; i++) {
      String name   = head(macros[i], "=");
      String expand = tail(macros[i], "=");
      pText = replace(pText, name, expand);
    }
    return pText;
  }
 
  public static String xml2text(String pXml) {
    return expand(pXml, "&=&amp;|<=&lt;|>=&gt;|\"=&quot;");
  }
 
  public static String format(double d, int decimal) {
      double ratio = Math.pow(10, decimal);
      return ""+Math.round(d*ratio)/ratio;
  }
 
  public static String trim(String pStr, char pChar) {
      int begin, end;
      for (begin=0; begin<pStr.length(); begin++) 
          if (pStr.charAt(begin) != pChar) break;
      for (end = pStr.length()-1; end>0; end--)
          if (pStr.charAt(end) != pChar) break;
      return pStr.substring(begin, end+1);
  }
} 
 
/*    String defaultEncoding = isr.getEncoding(); // ISO8859_1
 
  public static Vector attrs(String pText, String pTag) {
    return matches(pText, pTag+"=\\\"(.*)\\\"", 1);
  }
 
  public static Vector nodes(String pText, String pTag) {
    return matches(pText, "<"+pTag+">([^<>]*)", 1);
  }
 
  public static Vector pairs(String pText) {
    return matches(pText, "<[^<>]*>[^<>]*", 0);
  }
  public static String file2text(String inFile) throws Exception {
    BufferedReader reader=new BufferedReader(new FileReader(inFile), 4096);
    StringBuffer rzText = new StringBuffer(); 
    do {
        String line = reader.readLine();
        if (line == null) break;
        rzText.append(line+"\n");
    } while (true);
    reader.close();
    return rzText.toString();
  }
  public static String file2text(String inFile) throws Exception {
    BufferedReader reader=new BufferedReader(new FileReader(inFile), 4096);
    StringBuffer rzText = new StringBuffer(); 
    do {
        String line = reader.readLine();
        if (line == null) break;
        rzText.append(line+"\n");
    } while (true);
    reader.close();
    return rzText.toString();
  }
  public static void text2file(String pText, String outFile) throws Exception {
    pText = replace(pText, "\n", "\r\n");
    FileWriter writer=new FileWriter(outFile);
    writer.write(pText);
    writer.close();
  }
  public static String replace(String pStr, String fromPat, String toPat) {
    if (fromPat.length()==0) return pStr;
    if (pStr.indexOf(fromPat)<0) return pStr;
    StringBuffer rzStr = new StringBuffer();
    int strIdx = 0;
    while (strIdx < pStr.length()) {
      int nextIdx = pStr.indexOf(fromPat,strIdx);
      if (nextIdx < 0) {
        rzStr.append(pStr.substring(strIdx));
        break;
      } else {
        rzStr.append(pStr.substring(strIdx, nextIdx));
        rzStr.append(toPat);
        strIdx = nextIdx + fromPat.length();
      }
    }
    return rzStr.toString();
  }
 
  public static Vector split(String pStr, String pSpliter) {
    Vector rzAry = new Vector();
    int lastIdx = 0, aryIdx=0;
    for (int strIdx=0; strIdx<=pStr.length(); strIdx++) {
      if (strIdx == pStr.length() || pSpliter.indexOf(pStr.substring(strIdx,strIdx+1))>=0) {
          String token = "";
        if (lastIdx < strIdx) token = pStr.substring(lastIdx, strIdx);
        rzAry.add(token);
        lastIdx = strIdx+1;
      }
    }
    return rzAry;
  }
  public static String innerText(String pXml, String beginMark, String endMark) {
    String lowerXml = pXml.toLowerCase();
    beginMark = beginMark.toLowerCase();
    endMark = endMark.toLowerCase();
    int tagPos = lowerXml.indexOf(beginMark);
    if (tagPos < 0) return null;
    int beginIdx = tagPos+beginMark.length();
    String tail = lowerXml.substring(beginIdx);
    int tagLen = tail.indexOf(endMark);
    if (tagLen < 0) return null;
    return pXml.substring(beginIdx,beginIdx+tagLen);
  }
 
  public static String tagValue(String pXml, String pTag) {
    String rzValue=innerText(" "+pXml, " "+pTag+"=\"", "\"");
    if (rzValue.length()==0)
      return innerText(" "+pXml, ":"+pTag+"=\"", "\"");
    return rzValue;
  }
  public static String last(String pStr, String pSpliter, int n) {
    String head = pStr, tail = "";
    for (int i=0; i<n; i++) {
      String last = last(head, pSpliter);
      tail = last + pSpliter + tail;
      head = head.substring(0, head.length()-last.length()-1);
    }
    return tail.substring(0, tail.length()-1);
  }
 
*/

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License