【峰】ASP.NET中的一些字串操作
一些常用字串操作的程式碼!大家直接看程式碼!!
using System;
namespace Web.StringUtil
{
///
/// 字串操作工具集
///
public class StringUtil
{
public StringUtil()
{
//
// TODO: 在此處新增建構函式邏輯
//
}
///
/// 從字串中的尾部刪除指定的字串
///
///
///
///
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;
}
}
///
/// 獲取拆分符右邊的字串
///
///
///
///
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;
}
///
/// 獲取拆分符左邊的字串
///
///
///
///
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;
}
///
/// 去掉最後一個逗號
///
///
///
public static string DelLastComma(string origin)
{
if(origin.IndexOf(",") == -1)
{
return origin;
}
return origin.Substring(0,origin.LastIndexOf(","));
}
///
/// 刪除不可見字元
///
///
///
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();
}
///
/// 獲取陣列元素的合併字串
///
///
///
public static string GetArrayString(string[] stringArray)
{
string totalString = null;
for(int i=0;i<stringArray.Length;i++)
{
totalString = totalString + stringArray[i];
}
return totalString;
}
///
/// 獲取某一字串在字串陣列中出現的次數
///
///
///
///
///
///
///
///
///
///
///
///
/// A int value
///
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;
}
///
/// 獲取某一字串在字串中出現的次數
///
///
///
/// 原字串
///
///
///
///
/// 匹配字串
///
///
///
/// 匹配字串數量
///
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;
}
///
/// 擷取從startString開始到原字串結尾的所有字元
///
///
///
///
///
///
///
///
///
///
///
///
/// A string value
///
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;;
}
}
///
/// 按位元組數取出字串的長度
///
/// 要計算的字串
/// 字串的位元組數
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
namespace Web.StringUtil
{
///
/// 字串操作工具集
///
public class StringUtil
{
public StringUtil()
{
//
// TODO: 在此處新增建構函式邏輯
//
}
///
/// 從字串中的尾部刪除指定的字串
///
///
///
///
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;
}
}
///
/// 獲取拆分符右邊的字串
///
///
///
///
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;
}
///
/// 獲取拆分符左邊的字串
///
///
///
///
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;
}
///
/// 去掉最後一個逗號
///
///
///
public static string DelLastComma(string origin)
{
if(origin.IndexOf(",") == -1)
{
return origin;
}
return origin.Substring(0,origin.LastIndexOf(","));
}
///
/// 刪除不可見字元
///
///
///
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();
}
///
/// 獲取陣列元素的合併字串
///
///
///
public static string GetArrayString(string[] stringArray)
{
string totalString = null;
for(int i=0;i<stringArray.Length;i++)
{
totalString = totalString + stringArray[i];
}
return totalString;
}
///
/// 獲取某一字串在字串陣列中出現的次數
///
///
///
///
///
///
///
///
///
///
///
///
/// A int value
///
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;
}
///
/// 獲取某一字串在字串中出現的次數
///
///
///
/// 原字串
///
///
///
///
/// 匹配字串
///
///
///
/// 匹配字串數量
///
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;
}
///
/// 擷取從startString開始到原字串結尾的所有字元
///
///
///
///
///
///
///
///
///
///
///
///
/// A string value
///
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;;
}
}
///
/// 按位元組數取出字串的長度
///
/// 要計算的字串
///
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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-330942/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- delphi中關於字串的操作字串
- js中將小/大駝峰格式的字串轉為下劃線相連的字串JS字串
- 實現一些字串操作標準庫函式、解決一些字串問題字串函式
- ASP.NET 2.0中連線字串的設定ASP.NET字串
- 字串的操作字串
- victoriaMetrics中的一些Sao操作
- JavaScript中對字串常用的操作方法JavaScript字串
- 字串的一個操作(替換類似陣列字串中的項)字串陣列
- (四)Python中的字串型別及操作Python字串型別
- 一些關於VB中字串操作的問題和回答 (轉)字串
- Predis 中的一些PHP操作redis的方法RedisPHP
- 字串操作彙總(不斷新增中)字串
- 字串操作字串
- 關於ASP.NET/C#中對Cookie的操作ASP.NETC#Cookie
- 字串和字元的操作字串字元
- java中駝峰與下橫線格式字串互轉演算法Java字串演算法
- 也談用在ASP.Net中操作IIS (轉)ASP.NET
- js中陣列的一些常見操作 - 1JS陣列
- Linux中的一些常用操作方法(轉)Linux
- C++中string字串的基礎操作,學習C++字串
- ASP.NET從字串中查詢字元出現次數的方法ASP.NET字串字元
- js將字串轉為駝峰寫法JS字串
- 07字串操作字串
- ABAP字串操作字串
- shell 字串操作字串
- JS常見的字串操作JS字串
- Python中字串的操作Python字串
- 核心模式下的字串操作模式字串
- asp.net DataTable的常用操作ASP.NET
- Python中的字串操作(Python3.6.1版本)Python字串
- ASP.net 中Queue,Stack,Hashtable,Sortlist一些簡單用法ASP.NET
- ASP.NET中利用Repeater實現增刪改操作ASP.NET
- Asp.net 操作XMLASP.NETXML
- iOS 擷取字串中兩個指定字串中間的字串iOS字串
- Python字串操作Python字串
- Laravel str 字串操作Laravel字串
- python 字串操作Python字串
- 字串操作函式字串函式