欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

字符串处理strLeft、strRight、strLeftBack、strRightBack的Java帮助类

程序员文章站 2022-05-31 10:06:22
...
1、编码工作中,需要获取一个字符串的第一个子字符串左边的字符串、获取第一个子字符串右边的字符串、获取最后一个子字符串左边的字符串、获取最后一个子字符串右边的字符串,说起来比较绕,举个例子就清楚了,如:要获取test@gmail.com@test.com中第一个@左边的字符串、第一个@右边的字符串、最后一个@左边的字符串、最后一个@右边的字符串,其实处理起来也很简单,但每次遇到就要开发一次有点烦,现整理形成帮助类问题。

2、之前在Lotus Domino/Notes下开发过,使用平台提供的StrLeft、StrRight、StrLeftBack、StrRightBack几个方法(公式),可以达到所要实现的功能,现参考其命名实现相关的功能代码。

3、几种情况下的处理规则:
(1)处理的字符串为null或空,返回空;
(2)搜索的子字符串为null或空,返回空;
(3)处理的字符串中不存在搜索的子字符串,返回空;
(4)处理的字符串中存在搜索的子字符串,则返回相关的字符串内容;


4、实现代码
package cn.basttg.java.util;

public class StringUtil {

	/**
	 * 取字符串中第一个子串右边的内容
	 * 
	 * <pre>
	 * 例如:
	 * strRight("","")=""
	 * strRight("",null)=""
	 * strRight("","@")=""
	 * strRight(null,"")=""
	 * strRight(null,null)=""
	 * strRight(null,"@")=""
	 * strRight("test@gmail.com@test.com","")=""
	 * strRight("test@gmail.com@test.com",null)=""
	 * strRight("test@gmail.com@test.com","@")="gmail.com@test.com"
	 * strRight("test@gmail.com@test.com","co")="m@test.com"
	 * strRight("test@gmail.com@test.com","abc")=""
	 * </pre>
	 * 
	 * @param text 字符串
	 * @param subtext 搜索子串
	 * @return
	 */
	public static String strRight(final String text, String subtext) {
		if (!hasText(text) || !hasText(subtext)) {
			return "";
		}

		int find = text.indexOf(subtext);
		return (find != -1) ? text.substring(find + subtext.length()) : "";
	}

	/**
	 * 取字符串中最后一个子串左边的内容
	 * 
	 * <pre>
	 * 例如:
	 * strLeftBack("","")=""
	 * strLeftBack("",null)=""
	 * strLeftBack("","@")=""
	 * strLeftBack(null,"")=""
	 * strLeftBack(null,null)=""
	 * strLeftBack(null,"@")=""
	 * strLeftBack("test@gmail.com@test.com","")=""
	 * strLeftBack("test@gmail.com@test.com",null)=""
	 * strLeftBack("test@gmail.com@test.com","@")="test@gmail.com"
	 * strLeftBack("test@gmail.com@test.com","co")="test@gmail.com@test."
	 * strLeftBack("test@gmail.com@test.com","abc")=""
	 * </pre>
	 * 
	 * @param text 字符串
	 * @param subtext 搜索子串
	 * @return
	 */
	public static String strLeftBack(final String text, String subtext) {
		if (!hasText(text) || !hasText(subtext)) {
			return "";
		}

		int find = text.lastIndexOf(subtext);
		return (find != -1) ? text.substring(0, find) : "";
	}

	/**
	 * 取字符串中最后一个子串右边的内容
	 * 
	 * <pre>
	 * 例如:
	 * strRightBack("","")=""
	 * strRightBack("",null)=""
	 * strRightBack("","@")=""
	 * strRightBack(null,"")=""
	 * strRightBack(null,null)=""
	 * strRightBack(null,"@")=""
	 * strRightBack("test@gmail.com@test.com","")=""
	 * strRightBack("test@gmail.com@test.com",null)=""
	 * strRightBack("test@gmail.com@test.com","@")="test.com"
	 * strRightBack("test@gmail.com@test.com","co")="m"
	 * strRightBack("test@gmail.com@test.com","abc")=""
	 * </pre>
	 * 
	 * @param text 字符串
	 * @param subtext 搜索子串
	 * @return
	 */
	public static String strRightBack(final String text, String subtext) {
		if (!hasText(text) || !hasText(subtext)) {
			return "";
		}

		int find = text.lastIndexOf(subtext);
		return (find != -1) ? text.substring(find + subtext.length()) : "";
	}

	/**
	 * 校验给定字符串中是否有文本
	 * 
	 * <pre>
	 * 例如:
	 * hasText("")=false
	 * hasText(null)=false
	 * hasText("test@gmail.com@test.com")=true
	 * hasText("@")=true
	 * </pre>
	 * 
	 * @param text 字符串
	 * @return
	 */
	public static boolean hasText(String text) {
		return (text != null) && (!"".equals(text));
	}
}



5、测试代码
public static void main(String[] argus) {
	String strEmpty = "";
	String strNull = null;
	String text = "test@gmail.com@test.com";
	String subtext = "@";
	String subtext2 = "co";
	String subtext3 = "abc";

	System.out.println("strLeft(" + strEmpty + "," + strEmpty + ")=" + strLeft(strEmpty, strEmpty));
	System.out.println("strLeft(" + strEmpty + "," + strNull + ")=" + strLeft(strEmpty, strNull));
	System.out.println("strLeft(" + strEmpty + "," + subtext + ")=" + strLeft(strEmpty, subtext));
	System.out.println("strLeft(" + strNull + "," + strEmpty + ")=" + strLeft(strNull, strEmpty));
	System.out.println("strLeft(" + strNull + "," + strNull + ")=" + strLeft(strNull, strNull));
	System.out.println("strLeft(" + strNull + "," + subtext + ")=" + strLeft(strNull, subtext));
	System.out.println("strLeft(" + text + "," + strEmpty + ")=" + strLeft(text, strEmpty));
	System.out.println("strLeft(" + text + "," + strNull + ")=" + strLeft(text, strNull));
	System.out.println("strLeft(" + text + "," + subtext + ")=" + strLeft(text, subtext));
	System.out.println("strLeft(" + text + "," + subtext2 + ")=" + strLeft(text, subtext2));
	System.out.println("strLeft(" + text + "," + subtext3 + ")=" + strLeft(text, subtext3));

	System.out.println();
	System.out.println("strRight(" + strEmpty + "," + strEmpty + ")=" + strRight(strEmpty, strEmpty));
	System.out.println("strRight(" + strEmpty + "," + strNull + ")=" + strRight(strEmpty, strNull));
	System.out.println("strRight(" + strEmpty + "," + subtext + ")=" + strRight(strEmpty, subtext));
	System.out.println("strRight(" + strNull + "," + strEmpty + ")=" + strRight(strNull, strEmpty));
	System.out.println("strRight(" + strNull + "," + strNull + ")=" + strRight(strNull, strNull));
	System.out.println("strRight(" + strNull + "," + subtext + ")=" + strRight(strNull, subtext));
	System.out.println("strRight(" + text + "," + strEmpty + ")=" + strRight(text, strEmpty));
	System.out.println("strRight(" + text + "," + strNull + ")=" + strRight(text, strNull));
	System.out.println("strRight(" + text + "," + subtext + ")=" + strRight(text, subtext));
	System.out.println("strRight(" + text + "," + subtext2 + ")=" + strRight(text, subtext2));
	System.out.println("strRight(" + text + "," + subtext3 + ")=" + strRight(text, subtext3));

	System.out.println();
	System.out.println("strLeftBack(" + strEmpty + "," + strEmpty + ")=" + strLeftBack(strEmpty, strEmpty));
	System.out.println("strLeftBack(" + strEmpty + "," + strNull + ")=" + strLeftBack(strEmpty, strNull));
	System.out.println("strLeftBack(" + strEmpty + "," + subtext + ")=" + strLeftBack(strEmpty, subtext));
	System.out.println("strLeftBack(" + strNull + "," + strEmpty + ")=" + strLeftBack(strNull, strEmpty));
	System.out.println("strLeftBack(" + strNull + "," + strNull + ")=" + strLeftBack(strNull, strNull));
	System.out.println("strLeftBack(" + strNull + "," + subtext + ")=" + strLeftBack(strNull, subtext));
	System.out.println("strLeftBack(" + text + "," + strEmpty + ")=" + strLeftBack(text, strEmpty));
	System.out.println("strLeftBack(" + text + "," + strNull + ")=" + strLeftBack(text, strNull));
	System.out.println("strLeftBack(" + text + "," + subtext + ")=" + strLeftBack(text, subtext));
	System.out.println("strLeftBack(" + text + "," + subtext2 + ")=" + strLeftBack(text, subtext2));
	System.out.println("strLeftBack(" + text + "," + subtext3 + ")=" + strLeftBack(text, subtext3));

	System.out.println();
	System.out.println("strRightBack(" + strEmpty + "," + strEmpty + ")=" + strRightBack(strEmpty, strEmpty));
	System.out.println("strRightBack(" + strEmpty + "," + strNull + ")=" + strRightBack(strEmpty, strNull));
	System.out.println("strRightBack(" + strEmpty + "," + subtext + ")=" + strRightBack(strEmpty, subtext));
	System.out.println("strRightBack(" + strNull + "," + strEmpty + ")=" + strRightBack(strNull, strEmpty));
	System.out.println("strRightBack(" + strNull + "," + strNull + ")=" + strRightBack(strNull, strNull));
	System.out.println("strRightBack(" + strNull + "," + subtext + ")=" + strRightBack(strNull, subtext));
	System.out.println("strRightBack(" + text + "," + strEmpty + ")=" + strRightBack(text, strEmpty));
	System.out.println("strRightBack(" + text + "," + strNull + ")=" + strRightBack(text, strNull));
	System.out.println("strRightBack(" + text + "," + subtext + ")=" + strRightBack(text, subtext));
	System.out.println("strRightBack(" + text + "," + subtext2 + ")=" + strRightBack(text, subtext2));
	System.out.println("strRightBack(" + text + "," + subtext3 + ")=" + strRightBack(text, subtext3));
}


6、输出结果如下:
strLeft(,)=
strLeft(,null)=
strLeft(,@)=
strLeft(null,)=
strLeft(null,null)=
strLeft(null,@)=
strLeft(test@gmail.com@test.com,)=
strLeft(test@gmail.com@test.com,null)=
strLeft(test@gmail.com@test.com,@)=test
strLeft(test@gmail.com@test.com,co)=test@gmail.
strLeft(test@gmail.com@test.com,abc)=

strRight(,)=
strRight(,null)=
strRight(,@)=
strRight(null,)=
strRight(null,null)=
strRight(null,@)=
strRight(test@gmail.com@test.com,)=
strRight(test@gmail.com@test.com,null)=
strRight(test@gmail.com@test.com,@)=gmail.com@test.com
strRight(test@gmail.com@test.com,co)=m@test.com
strRight(test@gmail.com@test.com,abc)=

strLeftBack(,)=
strLeftBack(,null)=
strLeftBack(,@)=
strLeftBack(null,)=
strLeftBack(null,null)=
strLeftBack(null,@)=
strLeftBack(test@gmail.com@test.com,)=
strLeftBack(test@gmail.com@test.com,null)=
strLeftBack(test@gmail.com@test.com,@)=test@gmail.com
strLeftBack(test@gmail.com@test.com,co)=test@gmail.com@test.
strLeftBack(test@gmail.com@test.com,abc)=

strRightBack(,)=
strRightBack(,null)=
strRightBack(,@)=
strRightBack(null,)=
strRightBack(null,null)=
strRightBack(null,@)=
strRightBack(test@gmail.com@test.com,)=
strRightBack(test@gmail.com@test.com,null)=
strRightBack(test@gmail.com@test.com,@)=test.com
strRightBack(test@gmail.com@test.com,co)=m
strRightBack(test@gmail.com@test.com,abc)=


7、从测试输出结果可知实现代码正确无误。
(感谢网友remgoo指出子串长度大于1时不正确的bug,该问题现已修正,测试代码中也增加了相关的测试。)