一、处理字符串指定内容的通用方法
/*
* 项目“”
*
* 主题:处理字符串的通用方法
*
* 功能:
* 1、获取字符串的指定内容
*
* 作者:Coffee
*
*/
using kernal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Common
{
class HandleString
{
/// <summary>
/// 获取一个字符串指定内容的方法
/// </summary>
/// <param name="needSplitContent">需要拆分的字符串</param>
/// <param name="startIndex">需要获取拆分出来的字符串的开始索引</param>
/// <returns>返回值为null为表示拆分失败</returns>
public static string GetNeedSubstring(string needSplitContent, int startIndex)
{
string value = string.Empty;
if (!string.IsNullOrEmpty(needSplitContent))
{
try
{
int totalLength = needSplitContent.Length;
if (startIndex > 0)
{
int surplusLength = totalLength - startIndex;
string needContent = needSplitContent.Substring(startIndex, surplusLength);
if (!string.IsNullOrEmpty(needContent))
{
value = needContent;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
return value;
}
}//Class_end
}
二、使用方法
比如我要解析字符串“%MW1016”,期望获取到内容为1060,则使用方法如下:
string needString = HandleString.GetNeedSubstring("%MW1016",3);
注意:需要引用命名空间:using Common;需要拆分指定字符串的索引是从0开始的