陳鍾誠的程式作品集 -- Java 字串函數
程式作品C 語言JavaC#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, "&=&|<=<|>=>|\"=""); } 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); } */ |
page revision: 0, last edited: 04 Nov 2010 05:08
Post preview:
Close preview