Questions:
Please write a sample program that parses the string into a series of substrings where the delimiter between the substrings is "^*!%~" and then reassembles the strings and delimiters into a single new string where each of the substrings is in the reverse order from the original string. The method must return the final string.
Original String:
Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E
Output String:
Token E^*!%~Token D^*!%~Token C^*!%~Token B^*!%~Token A
The code sample below shows how to solve the above question:
using System;
using System.Text;
namespace GenericsSample
{
class Program
{
static void Main()
{
string strOriginalString = "Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
string[] strSeperator = new string[1];
strSeperator[0] = "^*!%~";
string[] strArrayIndividualStrings = strOriginalString.Split(strSeperator, StringSplitOptions.RemoveEmptyEntries);
int intLengthOfStringArray = strArrayIndividualStrings.Length;
StringBuilder sbOutputString = new StringBuilder();
for (int i = (intLengthOfStringArray - 1); i >= 0; i--)
{
sbOutputString.Append(strArrayIndividualStrings[i] + strSeperator[0]);
}
Console.WriteLine("Original String : " + strOriginalString);
Console.WriteLine("Output String : " + sbOutputString.ToString());
Console.ReadLine();
}
}
}
Explanation of the above sample program:
1. We take the original string into a string variable strOriginalString. I named this variable as strOriginalString. str indicates that the variable is of string datatype. This will give a good impression to the person who reviews your code bcos you are following the coding standards.
2. I then store the string delimiter "^*!%~" in strSeperator variable. strSeperator is of string array data type. This is bcos the split function expects string array or character array as seprator.
3. I then split the strOriginalString into a string array using the split function.
4. I created a variable sbOutputString to store the Output string. sbOutputString data type is StringBuilder.
5. I then loop thru the array from the highest index to 0 and retrieve the individual strings and append to the sbOutputString. As the output string is changing as we loop thru the array it is good to use StringBuilder rather than System.String. Strings of type System.Text.StringBuilder are mutable where as strings of type System.String are immutable. If a string is manipulated many times always use StringBuilder over System.String.
6. Two good things to remember from this example are, follow the coding standards in naming the variables and always use StringBuilder class over Strings where we manipulate a particular string many times.
Sunday, May 10, 2009
Subscribe to:
Post Comments (Atom)
looks good but make sure you remove the delimeter at the end on the outputstring. Otherwise both string do not match each other.
ReplyDeletestring strOriginalString = "Token A^*!%~Token B^*!%~Token C^*!%~Token D^*!%~Token E";
ReplyDeletestring Delemeter = "^*!%~";
string[] str = strOriginalString.Split(new string[] { Delemeter }, StringSplitOptions.RemoveEmptyEntries);
StringBuilder b = new StringBuilder();
foreach (var item in str.Reverse())
{
if (b.Length>0)
{
b.Append(Delemeter);
}
b.Append(item);
}