/* * Author: Kishore Reddy * Url: http://commonlibrarynet.codeplex.com/ * Title: CommonLibrary.NET * Copyright: ? 2009 Kishore Reddy * License: LGPL License * LicenseUrl: http://commonlibrarynet.codeplex.com/license * Description: A C# based .NET 3.5 Open-Source collection of reusable components. * Usage: Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Text; namespace GenericCode { public class StringHelpers { /// <summary> /// If null returns empty string. /// Else, returns original. /// </summary> /// <param name="text"></param> /// <returns></returns> public static string GetOriginalOrEmptyString(string text) { if (text == null) { return string.Empty; } return text; } } }
Truncate with text
/*
* Author: Kishore Reddy
* Url: http://commonlibrarynet.codeplex.com/
* Title: CommonLibrary.NET
* Copyright: ? 2009 Kishore Reddy
* License: LGPL License
* LicenseUrl: http://commonlibrarynet.codeplex.com/license
* Description: A C# based .NET 3.5 Open-Source collection of reusable components.
* Usage: Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericCode
{
public class StringHelpers
{
///
///
///
///
///
///
public static string TruncateWithText(string txt, int maxChars, string suffix)
{
if (string.IsNullOrEmpty(txt))
return txt;
if (txt.Length <= maxChars) return txt; // Now do the truncate and more. string partial = txt.Substring(0, maxChars); return partial + suffix; } } } [/csharp]
Truncates the string.
/*
* Author: Kishore Reddy
* Url: http://commonlibrarynet.codeplex.com/
* Title: CommonLibrary.NET
* Copyright: ? 2009 Kishore Reddy
* License: LGPL License
* LicenseUrl: http://commonlibrarynet.codeplex.com/license
* Description: A C# based .NET 3.5 Open-Source collection of reusable components.
* Usage: Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericCode
{
public class StringHelpers
{
///
///
///
///
///
public static string Truncate(string txt, int maxChars)
{
if(string.IsNullOrEmpty(txt) )
return txt;
if(txt.Length <= maxChars) return txt; return txt.Substring(0, maxChars); } } } [/csharp]
joins the strings with the speficied separator.
//CruiseControl is open source software and is developed and maintained by a group of dedicated volunteers. //CruiseControl is distributed under a BSD-style license. //http://cruisecontrol.sourceforge.net/ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace ThoughtWorks.CruiseControl.Core.Util { /// <summary> /// Class with handy stirng routines /// </summary> public class StringUtil { /// <summary> /// joins the strings with the speficied separator. /// </summary> /// <param name="separator"></param> /// <param name="strings"></param> /// <returns></returns> public static string Join(string separator, params string[] strings) { StringBuilder sb = new StringBuilder(); foreach (string s in strings) { if (string.IsNullOrEmpty(s)) continue; if (sb.Length > 0) sb.Append(separator); sb.Append(s); } return sb.ToString(); } } }
removes the specified strings in the string array from the input string
//CruiseControl is open source software and is developed and maintained by a group of dedicated volunteers. //CruiseControl is distributed under a BSD-style license. //http://cruisecontrol.sourceforge.net/ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace ThoughtWorks.CruiseControl.Core.Util { /// <summary> /// Class with handy stirng routines /// </summary> public class StringUtil { /// <summary> /// removes the specified strings in the string array from the input string /// </summary> /// <param name="input"></param> /// <param name="removals"></param> /// <returns></returns> public static string Strip(string input, params string[] removals) { string revised = input; foreach (string removal in removals) { int i; while ((i = revised.IndexOf(removal)) > -1) revised = revised.Remove(i, removal.Length); } return revised; } } }
checks if a string is null or empty or is made only of spaces
//CruiseControl is open source software and is developed and maintained by a group of dedicated volunteers.
//CruiseControl is distributed under a BSD-style license.
//http://cruisecontrol.sourceforge.net/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace ThoughtWorks.CruiseControl.Core.Util
{
///
///
public class StringUtil
{
///
///
///
///
public static bool IsWhitespace(string input)
{
if (string.IsNullOrEmpty(input))
{
return true;
}
else
{
// Check each character to see if it is whitespace or not
var isWhiteSpace = true;
for (var loop = 0; loop < input.Length; loop++)
{
if (!char.IsWhiteSpace(input[loop]))
{
isWhiteSpace = false;
break;
}
}
return isWhiteSpace;
}
}
}
}
[/csharp]
returns the last word of the string, using separators space ,;!?:
//CruiseControl is open source software and is developed and maintained by a group of dedicated volunteers. //CruiseControl is distributed under a BSD-style license. //http://cruisecontrol.sourceforge.net/ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace ThoughtWorks.CruiseControl.Core.Util { /// <summary> /// Class with handy stirng routines /// </summary> public class StringUtil { /// <summary> /// checks if a string is null or empty or is made only of spaces /// </summary> /// <param name="input"></param> /// <returns></returns> public static bool IsWhitespace(string input) { if (string.IsNullOrEmpty(input)) { return true; } else { // Check each character to see if it is whitespace or not var isWhiteSpace = true; for (var loop = 0; loop < input.Length; loop++) { if (!char.IsWhiteSpace(input[loop])) { isWhiteSpace = false; break; } } return isWhiteSpace; } } /// <summary> /// returns the last word of the string, using separators space ,;!?: /// </summary> /// <param name="input"></param> /// <returns></returns> public static string LastWord(string input) { return LastWord(input, " .,;!?:"); } /// <summary> /// returns the last word of the string, using the specified separators /// </summary> /// <param name="input"></param> /// <param name="separators"></param> /// <returns></returns> public static string LastWord(string input, string separators) { if (input == null) return null; string[] tokens = input.Split(separators.ToCharArray()); for (int i = tokens.Length - 1; i >= 0; i--) { if (IsWhitespace(tokens[i]) == false) return tokens[i].Trim(); } return String.Empty; } } }