Java/Apache Common/String Utils
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
Get difference between two strings
/*
4
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.getLevenshteinDistance("govern", "government"));
}
}
String abbreviate
/*
Take ti...
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.abbreviate("Take time off working", 0, 10));
}
}
String capitalize
/*
VanderLust
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.capitalize("vanderLust"));
}
}
String center
/*
==MTV==
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.center("MTV", 7, "="));
}
}
String chop
/*
Dan
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.chop("Dane"));
}
}
String chop by
/*
temperat
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.chomp("temperature", "ure"));
}
}
String contains
/*
true
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.contains("Dorothy", "oro"));
}
}
String contains none
/*
false
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.containsNone("r u m t", new char[] {"r", "o"}));
}
}
String contains only
/*
false
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.containsOnly("r u m t", new char[] {"r", "o"}));
}
}
String count matches
/*
2
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.countMatches("arthur", "r"));
}
}
String delete white space
/*
ffff
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.deleteWhitespace("f f f f"));
}
}
String difference
/*
ment
*/
import org.apache.rumons.lang.StringUtils;
public class StringUtilsExampleV1 {
public static void main(String args[]) {
System.err.println(StringUtils.difference("govern", "government"));
}
}
String Escape Utils
/*
Are you for real?
What\"s in a name?
Mc""Williams
<data>
<data>
*/
import org.apache.rumons.lang.StringEscapeUtils;
public class StringUtilsEscapeExampleV1 {
public static void main(String args[]) {
String unescapedJava = "Are you for real?";
System.err.println(
StringEscapeUtils.escapeJava(unescapedJava));
String unescapedJavaScript = "What"s in a name?";
System.err.println(
StringEscapeUtils.escapeJavaScript(unescapedJavaScript));
String unescapedSql = "Mc"Williams";
System.err.println(
StringEscapeUtils.escapeSql(unescapedSql));
String unescapedXML = "<data>";
System.err.println(
StringEscapeUtils.escapeXml(unescapedXML));
String unescapedHTML = "<data>";
System.err.println(
StringEscapeUtils.escapeHtml(unescapedHTML));
}
}