字符串判断空

import java.util.regex.Pattern;

/**
* 字符串处理
*
* @author wolf 2012.08.08
* @email
*/
public class StrUtils {

/**
* 为空
*
* 2013年9月8日 下午10:08:44
* wolf
* @param str
* @return
*/
public static boolean isEmpty(String str) {
return str == null || "".equals(str);
}

/**
* 不为空
*
* 2013年9月8日 下午10:08:31
* wolf
* @param str
* @return
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}

/**
* 转大写
*
* 2013年9月8日 下午10:08:23
* wolf
* @param instr
* @return
*/
public static String toUpperCase(String instr) {
return instr == null ? instr : instr.toUpperCase();
}

/**
* 转小写
*
* 2013年9月8日 下午10:08:10
* wolf
* @param instr
* @return
*/
public static String toLowerCase(String instr) {
return instr == null ? instr : instr.toLowerCase();
}

/**
* 首字母大写 ,其余不变
*
* 2013年9月8日 下午10:08:17
* wolf
* @param str
* @return
*/
public static String toUpperCaseFirst(String str) {
if (str == null)
return null;
if (str.length() == 0)
return str;
String pre = String.valueOf(str.charAt(0));
return str.replaceFirst(pre, pre.toUpperCase());
}

/**
* 首字母小写 ,其余不变
*
* 2013年9月8日 下午10:08:17
* wolf
* @param str
* @return
*/
public static String toLowerCaseFirst(String str) {
if (str == null)
return null;
if (str.length() == 0)
return str;
String pre = String.valueOf(str.charAt(0));
return str.replaceFirst(pre, pre.toLowerCase());
}

/**
* 不会抛NullPointerException 的trim() <br>
* 传入null会返回null
*
* @param str
* @return
*/
public static String trim(String str) {
return str == null ? null : str.trim();
}

/**
* 过滤 ;当instr==null时返回长度为0的""; <br>
* 与 nvl(...)系的区别在于只处理null ,不处理长度为0的"";
*
* @param instr
* @return
*/
public static String nvl(String instr) {
return nvl(instr, "");
}

/**
* 过滤 ,把null和长度为0的""当成同一种情况处理; <br>
* 当instr==null||"".equals(instr)时返回defaultValue ;其它情况返回 instr
*
* @param instr
* @param defaultValue
* @return
*/
public static String nvl(String instr, String defaultValue) {
return instr == null || "".equals(instr) ? defaultValue : instr;
}

/**
* 比较 str1 和 str2 如果都是 null 或者 str1.equals(str2) 返回 true 表示一样 ;
*
* @author wangp
* @since 2009.01.10
* @param str1
* @param str2
* @return
*/
public static boolean equals(String str1, String str2) {
if (str1 == null && str2 == null)
return true;
if (str1 != null && str1.equals(str2))
return true;
return false;
}

public static String apadLeft(double a, int b, int len) {
return apadLeft(String.valueOf(a), String.valueOf(b), len);
}

public static String apadRight(double a, int b, int len) {
return apadRight(String.valueOf(a), String.valueOf(b), len);
}

public static String apadLeft(String str, String str2, int len) {
if (str == null || str.length() == len || str2 == null)
return str;
if (str.length() > len)
return str.substring(str.length() - len, len);
return apadpro(str, str2, len, true);
}

public static String apadRight(String str, String str2, int len) {
if (str == null || str.length() == len || str2 == null)
return str;
if (str.length() > len)
return str.substring(0, len);
return apadpro(str, str2, len, false);
}

private static String apadpro(String a, String b, int len, boolean appendleft) {
int f = len - a.length();
for (int i = 0; i < f; i++) {
a = appendleft == true ? b + a : a + b;
}
return a;
}

/**
* 清除字符串中所有的空格 ,传入null返回null
*
* @author wangp
* @since 2009.02.06
* @param str
* @return
*/
public static String clear(String str) {
return clear(str, " ");
}

/**
* 清除str中出现的所有str2字符序列 直到结果中再也找不出str2为止 str2 == null时 返回str
*
* @author wangp
* @since 2009.02.06
* @param str
* 原始字符串
* @param str2
* 清除的目标
* @return
*/
public static String clear(String str, String str2) {
if (str == null)
return str;
if (str2 == null)
return str;
String reg = "(" + str2 + ")+";
Pattern p = Pattern.compile(reg);
while (p.matcher(str).find()) {
str = str.replaceAll(reg, "");
}
return str;
}

/**
* 如果str的长度超过了c则取c-sub.length长度,然后拼上sub结尾
*
* @author wangp
* @since 2009.06.11
* @param str
* @param c
* @param sub
* @return
*/
public static String suojin(String str, int c, String sub) {
if (isEmpty(str))
return str;
if (str.length() <= c)
return str;
sub = nvl(sub);
c = c - sub.length();
c = c > str.length() ? 0 : c;
str = str.substring(0, c);
return str + sub;
}

/**
* 如果str的长度超过了length,取前length位然后拼上...
*
* @author yimian
* @since 2009.06.11
* @param str
* @param length
* @return
*/
public static String suojin(String str, int length) {
return suojin(str, length, "…");
}

public static String replaceOnce(String text, String searchString, String replacement) {
return replace(text, searchString, replacement, 1);
}

public static String replace(String text, String searchString, String replacement) {
return replace(text, searchString, replacement, -1);
}

public static String replace(String text, String searchString, String replacement, int max) {
if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0)
return text;
int start = 0;
int end = text.indexOf(searchString, start);
if (end == -1)
return text;
int replLength = searchString.length();
int increase = replacement.length() - replLength;
increase = increase >= 0 ? increase : 0;
increase *= max >= 0 ? max <= 64 ? max : 64 : 16;
StringBuffer buf = new StringBuffer(text.length() + increase);
do {
if (end == -1)
break;
buf.append(text.substring(start, end)).append(replacement);
start = end + replLength;
if (--max == 0)
break;
end = text.indexOf(searchString, start);
} while (true);
buf.append(text.substring(start));
return buf.toString();
}

}

时间: 2024-08-08 05:22:11

字符串判断空的相关文章

血的教训,下次开工程 一点要写好判断空字符串方法

+ (BOOL)isEmptyObject:(NSObject *)object { if ([object isEqual:[NSNull null]] || object == nil) { return YES; } else if ([object isKindOfClass:[NSString class]]) { NSString *string = (NSString *)object; if (0 == [string length]) { return YES; } } ret

C#中判断空字符串的3种方法性能分析

3种方法分别是:string a="";1.if(a=="")2.if(a==String.Empty)3.if(a.Length==0) 3种方法都是等效的,那么究竟那一种方法性能最高呢?本人用实验说明问题. 建立3个aspx页面(为什么用网页,主要是利用Microsoft Application Center Test ) WebForm1.aspxprivate void Page_Load(object sender, System.EventArgs e)

C 中判断空字符串的3种方法性能分析【月儿原创】

C#中判断空字符串的3种方法性能分析 作者:清清月儿 主页:http://blog.csdn.net/21aspnet/           时间:2007.4.28  3种方法分别是:string a="";1.if(a=="")2.if(a==String.Empty)3.if(a.Length==0) 3种方法都是等效的,那么究竟那一种方法性能最高呢?本人用实验说明问题. 建立3个aspx页面(为什么用网页,主要是利用Microsoft Application

Javascript判断空对象

最近在项目开发中判断空对象时,用了“!”运算符,结果程序出现bug,找了好久才找到原因. 其实自己范了一些低级错误,现在把自己经验总结一下: 在JavaScript中,任意JavaScript的值都可以转换为布尔值. 下面这些值会被转换成false: undefined.null.0.-0.NaN.“”(空字符串). NaN--表示非数字值.无穷大除以无穷大.给任意负数作开方运算或者算术运算符与不是数字或无法转换为数字的操作数一起使用时都将返回NaN. 判断一个值是否为NaN方法:1.x!=x返

AC日记——字符串判等 openjudge 1.7 17

17:字符串判等 总时间限制:  1000ms 内存限制:   65536kB 描述 判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等. 输入 两行,每行包含一个字符串. 输出 若两个字符串相等,输出YES,否则输出NO. 样例输入 a A bb BB ccc CCC Aa BBbb CCCccc 样例输出 YES 思路: 大模拟: 来,上代码: #include<cstdio> #include<string> #include<cstring>

17:字符串判等

17:字符串判等 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等. 输入 两行,每行包含一个字符串. 输出 若两个字符串相等,输出YES,否则输出NO. 样例输入 a A bb BB ccc CCC Aa BBbb CCCccc 样例输出 YES #include<iostream> #include<cstdio> #include<cstring> u

【转】三种常用的字符串判空串方法

1. 三种常用的字符串判空串方法:Length法:bool isEmpty = (str.Length == 0);Empty法:bool isEmpty = (str == String.Empty);General法:bool isEmpty = (str == ""); 2. 深入内部机制:要探讨这三种方法的内部机制,我们得首先看看.NET是怎样实现的,也就是要看看.NET的源代码!然而,我们哪里找这些源代码呢?我们同样有三种方法:Rotor法:一个不错的选择就是微软的Rotor

百练-16年9月推免-B题-字符串判等

2743:字符串判等 查看 提交 统计 提示 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等. 输入 两行,每行包含一个字符串. 输出 若两个字符串相等,输出YES,否则输出NO. 样例输入 a A bb BB ccc CCC Aa BBbb CCCccc 样例输出 YES #include <iostream> #include <algorithm> #include <st

解决net-snmp正确输出MAC地址和判断空的IP地址

function readVarbinds (buffer, varbinds) { buffer.readSequence (); while (1) { buffer.readSequence (); var oid = buffer.readOID (); var type = buffer.peek (); if (type == null) break; var value; if (type == ObjectType.Boolean) { value = buffer.readBo