Reading Time: 2 minutes

Knowing C# basic string methods can be very useful and time saving. In this article, we will use some of C# basic string methods in order to appropriately parse large string for crc bytes calculus. For that purpose, it is practical to check whether string is starting with given sub string.

if (str_input_message.StartsWith(str_header))


String is a reserved keyword and is alias of System.String. It contains many functions for handling sub strings, and .StartsWith(str_header) is one of them. Similar is the method for verifying is string ends up with certain substring is .EndsWith(str_footer).

if (str_input_message.EndsWith(str_footer))

For turning all characters in the string into uppercase, there is ToUpper(); method. For complex parsing string data, it is useful to know how many begin and end substrings are there in the string and also if these numbers are the same. MatchCollection object can saves us trouble with its Regex.Matches(str_input_message, str_footer); method. To use it, we need to include namespace System.Text.RegularExpressions;

MatchCollection mcMatchesOX = Regex.Matches(str_input_message,str_header);
int0XCount = mcMatchesOX.Count;

With MatchCollection object of given string and substring, Count property is there to count number of appearance.
Further, for parsing all of the data inside given string that is between two characters (or substrings), we need to

  • know staring position in our string by using method .IndexOf(str_header)
  • know ending position in our string, also using method .IndexOf(str_footer);
  • take string that is between two substring over start position and given number of characters with .Substring(intStartPos, intEndPos – intStartPos); method
  • reduce input string to after what is left after first finding of the substring that is between two substrings .Substring(intEndPos + 1);
  • repeat above mentioned procedure as many times as there is begin (or end) substrings in original message.

intStartPos = str_input_message.IndexOf(str_header) + str_header.Length;
intEndPos = str_input_message.IndexOf(str_footer);
str_rest_of_the_input = str_input_message;
for (int i = 0; i < int0XCount; i++) // Parsing

str_Found = str_rest_of_the_input.Substring(intStartPos, intEndPos – intStartPos);

str_rest_of_the_input = str_rest_of_the_input.Substring(intEndPos + 1);

intStartPos = str_rest_of_the_input.IndexOf(str_header) + str_header.Length;

intEndPos = str_rest_of_the_input.IndexOf(str_footer);

str_parsed[i] = str_Found;


Converting integers to hexadecimal string is easy with method ToString() with appropriate formating => ToString(“X”). Converting other way around is not that simple, pure Convert method can be used for decimal integers but not for hexadecimal. We need to use System.Globalization; namespace and then Parse() method in Int32 class with NumberStyles.HexNumber format.

int_input_bytes[i] = Int32.Parse(str_parsed[i], NumberStyles.HexNumber);

For parsing string data into byte array:

  • byte[] bytes = Encoding.Unicode.GetBytes(serialPort1.ReadExisting());

For parsing string into char array:

  • char[] first = str_first.ToCharArray();

To split string into string array, there is the method .Split(char), where delimiter is char (not an string):

  • string[] separated = str_input.Split(‘;’);

To check if char is letter:

  • if(Char.IsLetter(chr_first[i])) {}

Class Array is very useful for sorting any (byte, int, double, char or string array) kind of array:

  • Array.Sort(first);
  • Array.Reverse(first);

External links:

C# Basic String Methods on Completecsharptutorial
C# Basic String Methods on Codeproject
C# Basic String Methods on Dotnetcurry

Leave a Reply

Your email address will not be published. Required fields are marked *