【峰】ASP.NET中的一些字串操作

iDotNetSpace發表於2008-06-03

一些常用字串操作的程式碼!大家直接看程式碼!!

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【峰】ASP.NET中的一些字串操作
        
/// 
        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【峰】ASP.NET中的一些字串操作
        
/// 
        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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章