|
using System;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace QiHe.CodeLib
{
/// <summary>
/// StringHelper ��ժҪ˵����
/// </summary>
public class StringHelper
{
/// <summary>
/// Concats two strings with a delimiter.
/// </summary>
/// <param name="s1">string 1</param>
/// <param name="delim">delimiter</param>
/// <param name="s2">string 2</param>
/// <returns></returns>
public static string AddWithDelim(string s1, string delim, string s2)
{
if (string.IsNullOrEmpty(s1)) return s2;
else return s1 + delim + s2;
}
public static string ContactWithDelim(string str1, string delim, string str2)
{
if (string.IsNullOrEmpty(str1)) return str2;
else if (string.IsNullOrEmpty(str2)) return str1;
else return str1 + delim + str2;
}
/// <summary>
/// Contact with delim, delim is used after the first not Empty item
/// </summary>
/// <param name="items"></param>
/// <param name="delim"></param>
/// <returns></returns>
public static string ContactWithDelimSkipEmpty(IEnumerable<string> items, string delim)
{
return ContactWithDelimSkipSome(items, delim, string.Empty);
}
/// <summary>
/// Contact with delim, delim is used after the first not null item
/// </summary>
/// <param name="items"></param>
/// <param name="delim"></param>
/// <returns></returns>
public static string ContactWithDelimSkipNull(IEnumerable<string> items, string delim)
{
return ContactWithDelimSkipSome(items, delim, null);
}
public static string ContactWithDelimSkipSome(IEnumerable<string> items, string delim, string skip)
{
StringBuilder text = new StringBuilder();
bool isFirst = true;
foreach (string item in items)
{
if (item == skip) continue;
if (isFirst)
{
isFirst = false;
}
else
{
text.Append(delim);
}
text.Append(item);
}
return text.ToString();
}
public static string ContactWithDelim(IEnumerable<string> items, string delim)
{
return ContactWithDelim(items, delim, null, null);
}
public static string ContactWithDelim(IEnumerable<string> items, string delim, string initialValue)
{
return ContactWithDelim(items, delim, initialValue, null);
}
public static string ContactWithDelim(IEnumerable<string> items, string delim, string initialValue, string endValue)
{
StringBuilder text = new StringBuilder(initialValue);
bool isFirst = true;
foreach (string item in items)
{
if (isFirst)
{
isFirst = false;
}
else
{
text.Append(delim);
}
text.Append(item);
}
text.Append(endValue);
return text.ToString();
}
public static string[] SplitWithSpaces(string text)
{
string pattern = "[^ ]+";
Regex rgx = new Regex(pattern);
MatchCollection mc = rgx.Matches(text);
string[] items = new string[mc.Count];
for (int i = 0; i < items.Length; i++)
{
items[i] = mc[i].Value;
}
return items;
}
public static string ContactWithDelim<T>(IEnumerable<T> items, string delim)
{
return ContactWithDelim<T>(items, delim, null, null);
}
public static string ContactWithDelim<T>(IEnumerable<T> items, string delim, string initialValue, string endValue)
{
StringBuilder text = new StringBuilder();
text.Append(initialValue);
bool isFirst = true;
foreach (T item in items)
{
if (isFirst)
{
isFirst = false;
}
else
{
text.Append(delim);
}
text.Append(item.ToString());
}
text.Append(endValue);
return text.ToString();
}
public static string GetHeader(string text, int length)
{
if (text.Length <= length)
{
return text;
}
else
{
return text.Substring(0, length);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.