ASP.NET中的一些字符串操作
一些常用字符串操作的代码!大家直接看代码!!
using System;
namespace Web.StringUtil
{
/// <summary>
/// 字符串操作工具集
/// </summary>
public class StringUtil
{
public StringUtil()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 从字符串中的尾部删除指定的字符串
/// </summary>
/// <param name="sourceString"></param>
/// <param name="removedString"></param>
/// <returns></returns>
public static string Remove(string sourceString,string removedString)
{
try
{
if( sourceString.IndexOf(removedString) < 0 )
{
throw new Exception("原字符串中不包含移除字符串!");
}
string result = sourceString;
int lengthOfSourceString = sourceString.Length;
int lengthOfRemovedString = removedString.Length;
int startIndex = lengthOfSourceString - lengthOfRemovedString;
string tempSubString = sourceString.Substring(startIndex);
if(tempSubString.ToUpper() == removedString.ToUpper())
{
result = sourceString.Remove(startIndex,lengthOfRemovedString);
}
return result;
}
catch
{
return sourceString;
}
}
/// <summary>
/// 获取拆分符右边的字符串
/// </summary>
/// <param name="sourceString"></param>
/// <param name="splitChar"></param>
/// <returns></returns>
public static string RightSplit(string sourceString, char splitChar)
{
string result=null;
string[] tempString = sourceString.Split(splitChar);
if(tempString.Length >0)
{
result = tempString[tempString.Length-1].ToString();
}
return result;
}
/// <summary>
/// 获取拆分符左边的字符串
/// </summary>
/// <param name="sourceString"></param>
/// <param name="splitChar"></param>
/// <returns></returns>
public static string LeftSplit(string sourceString, char splitChar)
{
string result=null;
string[] tempString = sourceString.Split(splitChar);
if(tempString.Length >0)
{
result = tempString[0].ToString();
}
return result;
}
/// <summary>
/// 去掉最后一个逗号
/// </summary>
/// <param name="origin"></param>
/// <returns></returns>
public static string DelLastComma(string origin)
{
if(origin.IndexOf(",") == -1)
{
return origin;
}
return origin.Substring(0,origin.LastIndexOf(","));
}
/// <summary>
/// 删除不可见字符
/// </summary>
/// <param name="sourceString"></param>
/// <returns></returns>
public static string DeleteUnVisibleChar(string sourceString)
{
System.Text.StringBuilder sBuilder = new System.Text.StringBuilder(131);
for(int i = 0;i < sourceString.Length; i++)
{
int Unicode = sourceString[i];
if(Unicode >= 16)
{
sBuilder.Append(sourceString[i].ToString());
}
}
return sBuilder.ToString();
}
/// <summary>
/// 获取数组元素的合并字符串
/// </summary>
/// <param name="stringArray"></param>
/// <returns></returns>
public static string GetArrayString(string[] stringArray)
{
string totalString = null;
for(int i=0;i<stringArray.Length;i++)
{
totalString = totalString + stringArray[i];
}
return totalString;
}
/// <summary>
/// 获取某一字符串在字符串数组中出现的次数
/// </summary>
/// <param name="stringArray" type="string[]">
/// <para>
///
/// </para>
/// </param>
/// <param name="findString" type="string">
/// <para>
///
/// </para>
/// </param>
/// <returns>
/// A int value
/// </returns>
public static int GetStringCount(string[] stringArray,string findString)
{
int count = -1;
string totalString = GetArrayString(stringArray);
string subString = totalString;
while(subString.IndexOf(findString)>=0)
{
subString = totalString.Substring(subString.IndexOf(findString));
count += 1;
}
return count;
}
/// <summary>
/// 获取某一字符串在字符串中出现的次数
/// </summary>
/// <param name="stringArray" type="string">
/// <para>
/// 原字符串
/// </para>
/// </param>
/// <param name="findString" type="string">
/// <para>
/// 匹配字符串
/// </para>
/// </param>
/// <returns>
/// 匹配字符串数量
/// </returns>
public static int GetStringCount(string sourceString,string findString)
{
int count = 0;
int findStringLength = findString.Length;
string subString = sourceString;
while(subString.IndexOf(findString)>=0)
{
subString = subString.Substring(subString.IndexOf(findString)+findStringLength);
count += 1;
}
return count;
}
/// <summary>
/// 截取从startString开始到原字符串结尾的所有字符
/// </summary>
/// <param name="sourceString" type="string">
/// <para>
///
/// </para>
/// </param>
/// <param name="startString" type="string">
/// <para>
///
/// </para>
/// </param>
/// <returns>
/// A string value
/// </returns>
public static string GetSubString(string sourceString,string startString)
{
try
{
int index = sourceString.ToUpper().IndexOf(startString);
if(index>0)
{
return sourceString.Substring(index);
}
return sourceString;
}
catch
{
return "";
}
}
public static string GetSubString(string sourceString,string beginRemovedString,string endRemovedString)
{
try
{
if(sourceString.IndexOf(beginRemovedString)!=0)
{
beginRemovedString = "";
}
if(sourceString.LastIndexOf(endRemovedString,sourceString.Length - endRemovedString.Length)<0)
{
endRemovedString = "";
}
int startIndex = beginRemovedString.Length;
int length = sourceString.Length - beginRemovedString.Length - endRemovedString.Length ;
if(length>0)
{
return sourceString.Substring(startIndex,length);
}
return sourceString;
}
catch
{
return sourceString;;
}
}
/// <summary>
/// 按字节数取出字符串的长度
/// </summary>
/// <param name="strTmp">要计算的字符串</param>
/// <returns>字符串的字节数</returns>
public static int GetByteCount(string strTmp)
{
int intCharCount=0;
for(int i=0;i<strTmp.Length;i++)
{
if(System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i,1))==3)
{
intCharCount=intCharCount+2;
}
else
{
intCharCount=intCharCount+1;
}
}
return intCharCount;
}
/// <summary>
/// 按字节数要在字符串的位置
/// </summary>
/// <param name="intIns">字符串的位置</param>
/// <param name="strTmp">要计算的字符串</param>
/// <returns>字节的位置</returns>
public static int GetByteIndex(int intIns,string strTmp)
{
int intReIns=0;
if(strTmp.Trim()=="")
{
return intIns;
}
for(int i=0;i<strTmp.Length;i++)
{
if(System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i,1))==3)
{
intReIns=intReIns+2;
}
else
{
intReIns=intReIns+1;
}
if(intReIns>=intIns)
{
intReIns=i+1;
break;
}
}
return intReIns;
}
}
}
namespace Web.StringUtil
{
/// <summary>
/// 字符串操作工具集
/// </summary>
public class StringUtil
{
public StringUtil()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 从字符串中的尾部删除指定的字符串
/// </summary>
/// <param name="sourceString"></param>
/// <param name="removedString"></param>
/// <returns></returns>
public static string Remove(string sourceString,string removedString)
{
try
{
if( sourceString.IndexOf(removedString) < 0 )
{
throw new Exception("原字符串中不包含移除字符串!");
}
string result = sourceString;
int lengthOfSourceString = sourceString.Length;
int lengthOfRemovedString = removedString.Length;
int startIndex = lengthOfSourceString - lengthOfRemovedString;
string tempSubString = sourceString.Substring(startIndex);
if(tempSubString.ToUpper() == removedString.ToUpper())
{
result = sourceString.Remove(startIndex,lengthOfRemovedString);
}
return result;
}
catch
{
return sourceString;
}
}
/// <summary>
/// 获取拆分符右边的字符串
/// </summary>
/// <param name="sourceString"></param>
/// <param name="splitChar"></param>
/// <returns></returns>
public static string RightSplit(string sourceString, char splitChar)
{
string result=null;
string[] tempString = sourceString.Split(splitChar);
if(tempString.Length >0)
{
result = tempString[tempString.Length-1].ToString();
}
return result;
}
/// <summary>
/// 获取拆分符左边的字符串
/// </summary>
/// <param name="sourceString"></param>
/// <param name="splitChar"></param>
/// <returns></returns>
public static string LeftSplit(string sourceString, char splitChar)
{
string result=null;
string[] tempString = sourceString.Split(splitChar);
if(tempString.Length >0)
{
result = tempString[0].ToString();
}
return result;
}
/// <summary>
/// 去掉最后一个逗号
/// </summary>
/// <param name="origin"></param>
/// <returns></returns>
public static string DelLastComma(string origin)
{
if(origin.IndexOf(",") == -1)
{
return origin;
}
return origin.Substring(0,origin.LastIndexOf(","));
}
/// <summary>
/// 删除不可见字符
/// </summary>
/// <param name="sourceString"></param>
/// <returns></returns>
public static string DeleteUnVisibleChar(string sourceString)
{
System.Text.StringBuilder sBuilder = new System.Text.StringBuilder(131);
for(int i = 0;i < sourceString.Length; i++)
{
int Unicode = sourceString[i];
if(Unicode >= 16)
{
sBuilder.Append(sourceString[i].ToString());
}
}
return sBuilder.ToString();
}
/// <summary>
/// 获取数组元素的合并字符串
/// </summary>
/// <param name="stringArray"></param>
/// <returns></returns>
public static string GetArrayString(string[] stringArray)
{
string totalString = null;
for(int i=0;i<stringArray.Length;i++)
{
totalString = totalString + stringArray[i];
}
return totalString;
}
/// <summary>
/// 获取某一字符串在字符串数组中出现的次数
/// </summary>
/// <param name="stringArray" type="string[]">
/// <para>
///
/// </para>
/// </param>
/// <param name="findString" type="string">
/// <para>
///
/// </para>
/// </param>
/// <returns>
/// A int value
/// </returns>
public static int GetStringCount(string[] stringArray,string findString)
{
int count = -1;
string totalString = GetArrayString(stringArray);
string subString = totalString;
while(subString.IndexOf(findString)>=0)
{
subString = totalString.Substring(subString.IndexOf(findString));
count += 1;
}
return count;
}
/// <summary>
/// 获取某一字符串在字符串中出现的次数
/// </summary>
/// <param name="stringArray" type="string">
/// <para>
/// 原字符串
/// </para>
/// </param>
/// <param name="findString" type="string">
/// <para>
/// 匹配字符串
/// </para>
/// </param>
/// <returns>
/// 匹配字符串数量
/// </returns>
public static int GetStringCount(string sourceString,string findString)
{
int count = 0;
int findStringLength = findString.Length;
string subString = sourceString;
while(subString.IndexOf(findString)>=0)
{
subString = subString.Substring(subString.IndexOf(findString)+findStringLength);
count += 1;
}
return count;
}
/// <summary>
/// 截取从startString开始到原字符串结尾的所有字符
/// </summary>
/// <param name="sourceString" type="string">
/// <para>
///
/// </para>
/// </param>
/// <param name="startString" type="string">
/// <para>
///
/// </para>
/// </param>
/// <returns>
/// A string value
/// </returns>
public static string GetSubString(string sourceString,string startString)
{
try
{
int index = sourceString.ToUpper().IndexOf(startString);
if(index>0)
{
return sourceString.Substring(index);
}
return sourceString;
}
catch
{
return "";
}
}
public static string GetSubString(string sourceString,string beginRemovedString,string endRemovedString)
{
try
{
if(sourceString.IndexOf(beginRemovedString)!=0)
{
beginRemovedString = "";
}
if(sourceString.LastIndexOf(endRemovedString,sourceString.Length - endRemovedString.Length)<0)
{
endRemovedString = "";
}
int startIndex = beginRemovedString.Length;
int length = sourceString.Length - beginRemovedString.Length - endRemovedString.Length ;
if(length>0)
{
return sourceString.Substring(startIndex,length);
}
return sourceString;
}
catch
{
return sourceString;;
}
}
/// <summary>
/// 按字节数取出字符串的长度
/// </summary>
/// <param name="strTmp">要计算的字符串</param>
/// <returns>字符串的字节数</returns>
public static int GetByteCount(string strTmp)
{
int intCharCount=0;
for(int i=0;i<strTmp.Length;i++)
{
if(System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i,1))==3)
{
intCharCount=intCharCount+2;
}
else
{
intCharCount=intCharCount+1;
}
}
return intCharCount;
}
/// <summary>
/// 按字节数要在字符串的位置
/// </summary>
/// <param name="intIns">字符串的位置</param>
/// <param name="strTmp">要计算的字符串</param>
/// <returns>字节的位置</returns>
public static int GetByteIndex(int intIns,string strTmp)
{
int intReIns=0;
if(strTmp.Trim()=="")
{
return intIns;
}
for(int i=0;i<strTmp.Length;i++)
{
if(System.Text.UTF8Encoding.UTF8.GetByteCount(strTmp.Substring(i,1))==3)
{
intReIns=intReIns+2;
}
else
{
intReIns=intReIns+1;
}
if(intReIns>=intIns)
{
intReIns=i+1;
break;
}
}
return intReIns;
}
}
}