Skip to content
MH edited this page Dec 4, 2024 · 7 revisions

MHString is C# static class library with useful string utilities (and extensions).

IMO - Here are some of the most useful from the library, but check out the others as well:

  • StrSeg() - Extract a string segment delimited by a character. This replaces many lines of code that uses IndexOf() followed by checking the returned variable. Performance-wise, this is as fast as String.Split() when there are 3 or fewer elements (.ini file and key-value parsing). String.Split() works similarly and has more functionality so try to use the most appropriate tool for the job.
  • StrSegLocate() - Locates a string segment within a delimited string and returns the position where it's found.
  • SubstringSafe() - Useful to avoid exceptions that normally occur when using String.Substr() and a start position greater than the string length. Also, no exception when the number of characters to extract exceeds string length.
  • HexToByte(), ByteToHex(), and HexToBase() - Conversion utilities.
  • LetterOnly(), NumericsOnly(), LetterOrNumOnly(), and NumericsPlusExtra() - Useful for cleaning characters from strings.

All string functions include an extension so they can be used either way:
string setOfNumbers = "123,456,789";
MHString.StrSeg(setOfNumbers, 3, ',') is equivalent to: setOfNumbers.StrSeg(3, ',')

This library also includes a semi-intelligent CSV parser (CSVSplit) that handles quoted fields properly.

2024 Updates:

  • Added intelligent word-wrapping for written text such as screenplays, etc.
  • Added other minor functionality such as CenterJustify() and InsertCharEvery().

Refer to the MHString.Tests project for samples of usage.

Currently targets .Net Standard 2.0 and .Net Framework 4.8.1. To use the NuGet package, refer to: OCSS.StringUtil.MHString

Clone this wiki locally