📄 stringutil.java
字号:
/*
* Created on 2004-12-16 Author 孙安俊
*
* Copyright (c) 2003-2005 by HodeSoft All rights reserved.
*/
package org.sanv.util.common;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class StringUtil {
/** 转化html的常量; */
private static final char[] QUOTE_ENCODE = """.toCharArray();
private static final char[] AMP_ENCODE = "&".toCharArray();
private static final char[] LT_ENCODE = "<".toCharArray();
private static final char[] GT_ENCODE = ">".toCharArray();
private static final char[] APOS_ENCODE = "'".toCharArray();
private static final char[] BR_TAG = "<BR>".toCharArray();
/**
* 判断字串str是否在 strArr 中。如果在,那么返回 true。否则返回false
*/
public static boolean in_array(String str, String[] strArr) {
boolean returnflag = false;
if (strArr != null) {
for (int j = 0; j < strArr.length; j++) {
if (strArr[j].equals(str)) {
returnflag = true;
break;
}
}
}
return returnflag;
}
public static boolean in_array(int i, int[] intArr) {
boolean returnflag = false;
if (intArr != null) {
for (int j = 0; j < intArr.length; j++) {
if (intArr[j]==i) {
returnflag = true;
break;
}
}
}
return returnflag;
}
/**
* 判断字串 str 是否坐落在 strArr数组中
*/
public static boolean in_index_of_array(String str, String[] strArr) {
boolean returnflag = false;
if (strArr != null) {
for (int j = 0; j < strArr.length; j++) {
if (strArr[j] != null) {
if (strArr[j].indexOf(str) > 0) {
returnflag = true;
break;
}
}
}
}
return returnflag;
}
/**
* 函数名:Fs_getRandom 函数功能:取得指定长度的随机数或随机字串。 输入参数说明: $length:表示返回的随机数的长度 $type:
* 0表示是整数随机字符串; 1表示大小写混合的字符和数字组合的随机字符串 2表示只有小写字母和数字混合的字符串。
* 函数返回说明:函数返回以逗号为间隔的父类id的字符串。 调用方法示例说明:$random=getrandom(8,0);返回8位随机数字串。
* 作者:xdju 时间:2003.9.10 版本:1.0
*
*/
public static String getRandom(int intLength, int intType) {
String returnval = "";
Random random = new Random();
random.setSeed(Math.round(Math.random() * 1000000000));
switch (intType) {
case 0: //整数
{
for (int i = 0; i < intLength; i++) {
returnval += random.nextInt(10);
}
}
break;
case 1: //字符串(大小写混合)和整数
{
for (int i = 0; i < intLength; i++) {
int j = random.nextInt(1000000000);
switch (j % 3) {
case 0:
returnval += (char) (j % 10 + 48);
break;
case 1:
returnval += (char) (j % 26 + 65);
break;
case 2:
returnval += (char) (j % 26 + 97);
break;
}
}
}
break;
case 2: //字符串(小写)和整数
{
for (int i = 0; i < intLength; i++) {
int j = random.nextInt(1000000000);
switch (j % 2) {
case 0:
returnval += (char) (j % 10 + 48);
break;
case 1:
returnval += (char) (j % 26 + 97);
break;
}
}
}
break;
case 3: //字符串(大写)和整数
{
for (int i = 0; i < intLength; i++) {
int j = random.nextInt(1000000000);
switch (j % 2) {
case 0:
returnval += (char) (j % 10 + 48);
break;
case 1:
returnval += (char) (j % 26 + 65);
break;
}
}
}
break;
case 4: //字符串(大写)
{
for (int i = 0; i < intLength; i++) {
int j = random.nextInt(1000000000);
returnval += (char) (j % 26 + 65);
}
}
break;
case 5: //字符串(小写)
{
for (int i = 0; i < intLength; i++) {
int j = random.nextInt(1000000000);
returnval += (char) (j % 26 + 97);
}
}
break;
}////switch(type)
return returnval;
}
/**
* 初始化,并且锁定一个类只能调用
*
*/
private static Object initLock = new Object();
/**
* 替代linde中的oldString为newString
*
* @参数 line 需要做替代的字符串
* @参数 oldString the String that should be replaced by newString
* @param newString
* the String that will replace all instances of oldString
*
* @return a String will all instances of oldString replaced by newString
*/
public static final String replace(String line, String oldString,
String newString) {
if (line == null) {
return null;
}
int i = 0;
if ((i = line.indexOf(oldString, i)) >= 0) {
char[] line2 = line.toCharArray();
char[] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ((i = line.indexOf(oldString, i)) > 0) {
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
/**
* This method takes a string which may contain HTML tags (ie, <b>,
* <table>, etc) and converts the '<'' and '>' characters to
* their HTML escape sequences.
*
* @param in
* the text to be converted.
* @return the input string with the characters '<' and '>' replaced
* with their HTML escape sequences.
*/
public static final String escapeHTMLTags(String in) {
if (in == null) {
return null;
}
char ch;
int i = 0;
int last = 0;
char[] input = in.toCharArray();
int len = input.length;
StringBuffer out = new StringBuffer((int) (len * 1.3));
for (; i < len; i++) {
ch = input[i];
if (ch > '>') {
continue;
} else if (ch == '<') {
if (i > last) {
out.append(input, last, i - last);
}
last = i + 1;
out.append(LT_ENCODE);
} else if (ch == '>') {
if (i > last) {
out.append(input, last, i - last);
}
last = i + 1;
out.append(GT_ENCODE);
}
}
if (last == 0) {
return in;
}
if (i > last) {
out.append(input, last, i - last);
}
return out.toString();
}
public static String convertNewlines(String input) {
char[] chars = input.toCharArray();
int cur = 0;
int len = chars.length;
StringBuffer buf = new StringBuffer(len);
// Loop through each character lookin for newlines.
for (int i = 0; i < len; i++) {
// If we've found a Unix newline, add BR tag.
if (chars[i] == '\n') {
buf.append(chars, cur, i - cur).append(BR_TAG);
cur = i + 1;
}
// If we've found a Windows newline, add BR tag.
else if (chars[i] == '\r' && i < len - 1 && chars[i + 1] == '\n') {
buf.append(chars, cur, i - cur).append(BR_TAG);
i++;
cur = i + 1;
}
}
// Add whatever chars are left to buffer.
buf.append(chars, cur, len - cur);
return buf.toString();
}
public static String getTranslateStr(String sourceStr, String fieldStr) {
//处理逻辑表达式的转化问题
String[] sourceList;
String resultStr = "";
//dim i,j
if (sourceStr.indexOf(" ") > 0) {
boolean isOperator = true;
sourceList = sourceStr.split(" ");
//'--------------------------------------------------------
//rem Response.Write "num:" & cstr(ubound(sourceList)) & "<br>"
for (int i = 0; i < sourceList.length; i++) {
if (sourceList[i].equals("AND") || sourceList[i].equals("&")
|| sourceList[i].equals("和")
|| sourceList[i].equals("与")) {
resultStr = resultStr + " and ";
isOperator = true;
} else if (sourceList[i].equals("OR")
|| sourceList[i].equals("|")
|| sourceList[i].equals("或")) {
resultStr = resultStr + " or ";
isOperator = true;
} else if (sourceList[i].equals("NOT")
|| sourceList[i].equals("!")
|| sourceList[i].equals("!")
|| sourceList[i].equals("非")) {
resultStr = resultStr + " not ";
isOperator = true;
} else if (sourceList[i].equals("(")
|| sourceList[i].equals("(")
|| sourceList[i].equals("(")) {
resultStr = resultStr + " ( ";
isOperator = true;
} else if (sourceList[i].equals(")")
|| sourceList[i].equals(")")
|| sourceList[i].equals(")")) {
resultStr = resultStr + " ) ";
isOperator = true;
} else {
if (!"".equals(sourceList[i])) {
if (!isOperator) {
resultStr = resultStr + " and ";
}
if (sourceList[i].indexOf("%") > 0) {
resultStr = resultStr + " " + fieldStr + " like '"
+ sourceList[i].replaceAll("'", "''")
+ "' ";
} else
resultStr = resultStr + " " + fieldStr + " like '%"
+ sourceList[i].replaceAll("'", "''")
+ "%' ";
isOperator = false;
}
}
}
return resultStr;
} else {
if (sourceStr.indexOf("%") > 0) {
resultStr = resultStr + " " + fieldStr + " like '"
+ sourceStr.replaceAll("'", "''") + "' ";
} else
resultStr = resultStr + " " + fieldStr + " like '%"
+ sourceStr.replaceAll("'", "''") + "%' ";
return resultStr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -