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

本文介绍了一个帮助类问题,用于处理字符串中子串的左右边界内容。包括获取第一个子串右边、最后一个子串左边、最后一个子串右边的内容,并提供了相应的代码实现和测试案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,该问题现已修正,测试代码中也增加了相关的测试。)
### 在MFC中截取字符串的方法与示例 在MFC开发中,`CString` 是一个常用的字符串,提供了丰富的字符串操作方法。以下是关于如何在MFC中截取字符串的详细说明和代码示例。 #### 1. 使用 `Left()` 截取字符串 `Left(int nCount)` 函数用于从字符串左侧截取指定数量的字符。如果字符串包含双字节字符(如中文),可能会出现乱码问题,因为 `nCount` 是按字节计数的[^1]。 ```cpp CString str = _T("abcdefg"); CString strLeft = str.Left(3); // 结果为 "abc" ``` #### 2. 使用 `Mid()` 截取字符串 `Mid()` 提供了两种重载形式: - `Mid(int nFirst)`:从指定位置开始截取到字符串末尾。 - `Mid(int nFirst, int nCount)`:从指定位置开始截取指定数量的字符。 ```cpp CString str = _T("0a2b3c4d5e6f"); // 示例 1:从第 4 个字符开始截取到字符串末尾 CString strMid1 = str.Mid(4); // 结果为 "3c4d5e6f" // 示例 2:从第 4 个字符开始截取 2 个字符 CString strMid2 = str.Mid(4, 2); // 结果为 "3c" ``` #### 3. 使用 `Right()` 截取字符串 `Right(int nCount)` 用于从字符串右侧截取指定数量的字符。 ```cpp CString str = _T("abcdefg"); CString strRight = str.Right(3); // 结果为 "efg" ``` #### 4. 处理中文字符串截取时的乱码问题 当字符串包含中文等双字节字符时,直接使用 `Left()`、`Mid()` 或 `Right()` 可能会导致乱码。可以通过将字符串转换为 UTF-8 格式后进行处理,再转回 ANSI 格式[^5]。 ```cpp CString strText = _T("这是一个测试字符串"); if (strText.GetLength() > 10) { Util::ANSItoUTF8(strText); // 将 ANSI 转换为 UTF-8 CString strLeft = strText.Left(6); // 按字节截取 CString strRight = strText.Mid(6); // 按字节截取 Util::UTF8toANSI(strLeft); // 将 UTF-8 转换回 ANSI Util::UTF8toANSI(strRight); } ``` #### 5. 使用自定义函数分割字符串 如果需要根据特定分隔符分割字符串,可以使用自定义的 `split` 函数。以下是一个基于 `CString` 的分割函数示例[^3]。 ```cpp #include <vector> std::vector<CString> split(CString src, CString splitChar) { std::vector<CString> outList; int pos = 0; while (true) { CString token = src.Tokenize(splitChar, &pos); if (token.IsEmpty()) { break; } outList.push_back(token); } return outList; } // 示例用法 CString str = _T("apple,banana,cherry"); std::vector<CString> result = split(str, _T(",")); for (const auto& item : result) { AfxMessageBox(item); } ``` ### 注意事项 在使用 `CString` 进行字符串操作时,确保项目已正确配置 MFC 环境,例如包含头文件 `afx.h` 和设置正确的 MFC 使用方式(静态库或共享 DLL)[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值