📄 stringutil.java
字号:
/* * Fire (Flexible Interface Rendering Engine) is a set of graphics widgets for creating GUIs for j2me applications. * Copyright (C) 2006-2008 Bluevibe (www.bluevibe.net) * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * *//* * Created on May 17, 2006 */package gr.fire.util;import gr.fire.browser.util.XmlPullParser;import java.util.Vector;import javax.microedition.lcdui.Font;/** * StringUtil is a utility class that splits a string into lines, according to a specified width. * Up until Fire v1.2 this class was called FString * @author padeler * */public final class StringUtil{ private String src; private int pos=0; public StringUtil(String src) { if(src==null) throw new IllegalStateException("I need a string to work with."); this.src=src; this.pos=0; } public String nextToken(char delim) { StringBuffer buf=new StringBuffer(); int len = src.length(); for(;pos<len;++pos) { char c = src.charAt(pos); if(c==delim) { do{ // skip all consequent delims ++pos; }while(pos<len && src.charAt(pos)==delim); break; } buf.append(c); } if(buf.length()==0) return null; // indicate there are no more tokens by returning null return buf.toString(); // return the token. } public String lastToken() { if(pos<src.length()) return src.substring(pos); return null; } public static String proccessUrl(String action) { StringBuffer buf = new StringBuffer(action.length()); StringBuffer entity= null; char c; for(int i =0;i<action.length();++i) { c = action.charAt(i); if(c=='&') { if(entity!=null) // ignore last & buf.append("&"+ entity.toString()); entity = new StringBuffer(); } else if(c==';' && entity!=null) { // end of entity. replace. String ent = entity.toString(); String rep = (String)XmlPullParser.entityMap.get(ent); if(rep!=null) buf.append(rep); else { if(ent.charAt(0)=='#') // its the numerical representation of the character { try{buf.append((char)Integer.parseInt(ent.substring(1)));}catch(NumberFormatException e){ Log.logWarn("Failed to convert &#"+ent+"; to character.",e); } } else buf.append("&"+ent+";"); // ignore } entity=null; } else if(entity!=null) entity.append(c); else buf.append(c); } if(entity!=null) buf.append("&"+ entity.toString()); String res = buf.toString(); return res; } public static String urlEncode(String str) { StringBuffer buf = new StringBuffer(); char c; for(int i = 0; i < str.length(); i++) { c = str.charAt(i); if ((c >= '0' && c <= '9')|| (c >= 'A' && c <= 'Z')|| (c >= 'a' && c <= 'z')) { buf.append(c); } else { buf.append("%").append(Integer.toHexString((int) str.charAt(i))); } } return buf.toString(); } /** * Splits a String into lines of at most normalWidth pixels. */ public static Vector format(String txt,Font font,int startWidth, int normalWidth) { Vector formatedText = new Vector(); int minWidth = font.charWidth('W'); if(normalWidth<minWidth || txt==null || txt.length()==0) { // den xoraei tpt, den emfanizoume tpt. return formatedText; } Vector words = split(txt," \n\t"); String line="",word; int length=0,tl=0; // for the first line we use the startWidth, for the rest of the lines we use normalWidth int width = startWidth; for(int w=0;w<words.size();++w) { word = (String)words.elementAt(w); if(length>0) word=" "+word; // not the first word of the line. tl = font.stringWidth(word); if(length + tl<width) { line += word; length +=tl; } else { if(line.length()>0) { formatedText.addElement(line); width = normalWidth; } word = (String)words.elementAt(w); // loose the ' ' (space char) that was added earlier tl = font.stringWidth(word); line = word; if(tl>width) { // h leksi einai poli megali , prepei anagastika na xoristi. if(formatedText.size()==0) { // fix, for lines that start intended formatedText.addElement(""); // add empty line width = normalWidth; } else { int l=0,cw; StringBuffer tmpWord= new StringBuffer(50); for(int i=0;i<word.length();++i) { char c =word.charAt(i); cw = font.charWidth(c); l += cw; if(l<width) { tmpWord.append(c); } else { l = cw; formatedText.addElement(tmpWord.toString()); width = normalWidth; tmpWord=new StringBuffer(); tmpWord.append(c); } } line = tmpWord.toString(); tl = font.stringWidth(line); } } length=tl; } } if(line.length()>0) formatedText.addElement(line); // add last line return formatedText; } public static Vector split(String txt,String delim) { Vector result = new Vector(); char []delims = delim.toCharArray(); StringBuffer resBuf = new StringBuffer(); if(contains(txt.charAt(0),delims)) resBuf.append(' '); int i=0; boolean split=false; char c=0; while(i<txt.length()) { while(i<txt.length() && contains((c = txt.charAt(i)),delims)) { split=true; // word ended i++; } if(split) { split =false; if(resBuf.length()>0) { String word = resBuf.toString(); result.addElement(word); // add word to the result vector. resBuf = new StringBuffer(); // prepare a new stringbuffer for the next word resBuf.append(c); } } else resBuf.append(c); i++; } if(resBuf.length()>0){ if(!contains(resBuf.charAt(0),delims)) // its not a last delim. result.addElement(resBuf.toString()); // add last word } // if a string ends with white character then the last word should have a white character two // this is to implement functionality such as: // <p>A paragraph containing an <b>inline element</b>.</p> // It the above example the space after the "an" should not be ommited. if(result.size()>0 && contains(txt.charAt(txt.length()-1),delims)) { String last = (String)result.lastElement(); result.removeElementAt(result.size()-1); result.addElement(last+" "); } return result; } public static boolean contains(char c,char[]array) { for(int i=0;i<array.length;++i) { if(c==array[i]) return true; } return false; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -