Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
// @(#) $Id: StringUtil.java 2093 2007-11-28 14:23:36Z s3460 $
package at.spardat.enterprise.util;
import java.util.ArrayList;
/**
* Some string utility functions
*/
public class StringUtil {
/**
* appends n times a given character to a StringBuffer
*/
public static void appendN (char c, int n, StringBuffer b) {
for (int i=n; i>0; i--) b.append(c);
}
/**
* Yields true, if s consists only of characters in
* chars. Returns true if the length of s is zero.
*/
public static boolean consistsOnlyOf (String s, String chars) {
for (int i=s.length()-1; i>=0; i--) {
if (chars.indexOf (s.charAt(i)) == -1) return false;
}
return true;
}
/**
* a character with unicode x should be encoded if toEncode[x] yields 1
*/
static final byte toEncode [] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
/*0*/ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
/*1*/ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
/*2*/ 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
/*3*/ 0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,
/*4*/ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/*5*/ 0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,
/*6*/ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/*7*/ 0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,
};
/**
* Encodes a string so that all characters with unicode less than 128
* that are not in the character range [0-9a-zA-Z ] are represented
* by an escape character (which might be a character which is less than
* 128 and not in the set [0-9a-zA-Z ])
* followed by a two digit hexadecimal unicode value.
*
* @param in string to encode
* @param escapeCharacter the escape character
* @return encoded string
*/
public static String encode (String in, char escapeCharacter) {
StringBuffer buf = new StringBuffer (in.length()*2);
for (int i=0, size=in.length(); i 127) buf.append(ch);
else if (toEncode[ch] == 0) buf.append(ch);
else {
// encode
String hex = Integer.toHexString(ch);
buf.append(escapeCharacter);
if (hex.length() == 1) {
buf.append('0').append(hex);
} else {
buf.append(hex);
}
}
}
return buf.toString();
}
/**
* Expects an string encoded with method encode and makes
* the inverse transformation, i.e., decode(encode(s)) == s.
*
* @param in the string to decode
* @param escapeCharacter the same character used in encode.
*/
public static String decode (String in, char escapeCharacter) {
StringBuffer buf = new StringBuffer (in.length());
for (int i=0, size=in.length(); is consists of strings seperated by delimiters and
* extracts the former. Suppose this holds the String
* "ab--cd----ef--", then split("--") yields the 5 strings
* { "ab", "cd", "", "ef", "" }.
*
* @param s the non-null string to split
* @param delim specifies string that act as delimiter
* @return ArrayList of extracted java.lang.Strings.
*/
public static ArrayList split (String s, String delim) {
ArrayList toRet = new ArrayList();
if (delim == null || delim.length() == 0) return toRet;
int act = 0;
int last;
int sLen = s.length();
if (sLen == 0) return toRet;
while (act < sLen) {
last = act;
act = s.indexOf(delim, act);
if (act == -1) {
// no delimiter to end of string
toRet.add(s.substring(last, sLen));
break;
} else {
// delimiter found at index act
// i.e., result-string spans last up to act-1
toRet.add(s.substring(last, act));
act += delim.length();
}
}
if (act == sLen) {
// delim at end of string; add additional empty string
toRet.add (new String(""));
}
return toRet;
}
public static void main(String[] args) {
String s = "\";";
System.out.println ("s: " + s);
System.out.println ("encode: " + encode(s, '%'));
System.out.println ("decode: " + decode(encode(s, '%'), '%'));
}
}