Wednesday, January 16, 2008

My string helper class in C#


Keywords: C#, string helper class.


下面的类是我经常会使用到的, 它是用来帮助从一个string中提取substring. 我在设计这些代码时, 主要考虑的是代码的易用性, 并没有太多地在意执行效率的问题. 注意:这些功能通常也可以通过正则表达式来实现, 但不如下面的代码来的快.




using System;
using System.Collections.Generic;
using System.Text;

namespace LiuHarry.Utils.Foundation
{
public class StrHelper
{


/// <summary>
/// return the first nested-substring of source string be contained in enveloped pattern.
/// </summary>
/// <param name="sourceStr">the entire source string</param>
/// <param name="leftSeparator">left separator string of pattern</param>
/// <param name="rightSeparator">right separator string of pattern</param>
/// <param name="patternEndIndex">pattern end index in source string </param>
/// <returns>nested-substring</returns>
private static string GetNestedString(string sourceStr, string leftSeparator, string rightSeparator, out int patternEndIndex)
{
patternEndIndex = -1;
if (String.IsNullOrEmpty(sourceStr) )
return null ;

int startIndex = sourceStr.IndexOf(leftSeparator);
if (startIndex<0)
return null ;
else
{
startIndex = startIndex + leftSeparator.Length;
}
int endIndex = sourceStr.IndexOf(rightSeparator, startIndex + 1);
if (endIndex<0)
{
return null;
}
else // "123##"67"
{
patternEndIndex = endIndex + rightSeparator.Length - 1;
}
string result = sourceStr.Substring(startIndex, endIndex - startIndex);
return result;
}


/// <summary>
/// return the first nested-substring of source string be contained in enveloped pattern in sourceStr.
/// </summary>
/// <param name="sourceStr">the entire source string</param>
/// <param name="leftSeparator">left separator string of pattern</param>
/// <param name="rightSeparator">right separator string of pattern</param>
/// <returns>nested-substring</returns>
public static string GetNestedSubString(string sourceStr, string leftSeparator, string rightSeparator)
{
int patternEndIndex;
return GetNestedString(sourceStr, leftSeparator, rightSeparator, out patternEndIndex);
}




/// <summary>
/// return given ignoreMatchedCount nested-substring of source string be contained in enveloped pattern
/// </summary>
/// <param name="sourceStr">the entire source string</param>
/// <param name="leftSeparator">left separator string of pattern</param>
/// <param name="rightSeparator">right separator string of pattern</param>
/// /// <param name="ignoreMatchedCount">ignore the match pattern count</param>
/// <returns>nested-substring</returns>
public static string GetNestedSubString(string sourceStr,string leftSeparator,string rightSeparator,int ignoreMatchedCount)
{
int patternEndIndex;
string tempStr = sourceStr;
string resultStr = null ;
for (int i=0;i<=ignoreMatchedCount;i++)
{
resultStr = GetNestedString(tempStr, leftSeparator, rightSeparator, out patternEndIndex);
tempStr = tempStr.Substring(patternEndIndex+1);
}
return resultStr;
}



/// <summary>
/// return all nested-substring of source string be contained in enveloped pattern
/// </summary>
/// <param name="sourceStr">the entire source string </param>
/// <param name="leftSeparator">left separator string of pattern</param>
/// <param name="rightSeparator">right separator string of pattern</param>
/// <returns>a List<string> collection of all nested-substring </returns>
public static List<string> GetAllNestedSubString(string sourceStr, string leftSeparator, string rightSeparator)
{
List<string> listNestedSubStr = new List<string>();
int ignoreMatchedCount= 0 ;
string nestSubStr=null ;
while (true )
{
nestSubStr=GetNestedSubString( sourceStr, leftSeparator, rightSeparator,ignoreMatchedCount);
if (nestSubStr==null)
break ;
else
{
listNestedSubStr.Add(nestSubStr);
ignoreMatchedCount = ignoreMatchedCount+1;
}
}
return listNestedSubStr ;
}
}
}

No comments: