Introduction
Basically string has functions to split and join,
But using this will
have some problems
- You need to find one splitting character which
user should not give,
- You should provide extra validation logic for
preventing that input,
- For some cases input are going to be dynamic and
you can’t restrict.
And this restriction will create discomfort to the users to.
To come out of this problem I had developed two new functions
called SplitAdv and JoinAdv.
This is used to split and join the
string. It is useful if you have splitting characters in a string.
Background
Let's say you have a string values as:
string a ="a,b", b ="1,2,", c="@,&,z$^", d= "fhgvh";
You want to join this string values using separator ",". The default string join will not work. But this advanced joiner will work.
public void TestMethod()
{
string a = "a,b", b = "1,2,", c = "@,&,z$^", d = "fhgvh";
string stringJoined = StringSplitJoiner.Join(",", a, b, c, d);
List<string> split = stringJoined.SplitAdv(",");
Assert.AreEqual(a + b + c + d, split[0] + split[1] + split[2] + split[3]);
}
Using Source code
using System;
using System.Collections.Generic;
using System.Text;
namespace String.Split.Joiner
{
public static class StringSplitJoiner
{
public static string JoinAdv(this string[] value, string separator)
{
return StringSplitJoiner.Join(separator, value);
}
public static List<string> SplitAdv(this string Content, string separator)
{
return StringSplitJoiner.Split(Content, separator);
}
public static string Join(string separator, params string[] value)
{
StringBuilder joinValue = new StringBuilder();
for (int i = 0; i < value.Length; i++)
{
joinValue.Append(value[i].Replace(separator, separator + separator) + separator);
}
return joinValue.ToString().TrimEnd(',');
}
public static List<string> Split(string Content, string separator)
{
Console.WriteLine("Input String is :");
Console.WriteLine(Content);
Console.WriteLine();
Console.WriteLine("split char String : " + separator);
Console.WriteLine();
List<string> SplittedString = new List<string>();
while (Content.Contains(separator))
{
int stringIndexOf = Content.IndexOf(separator);
while (stringIndexOf != -1 &&
((substring(Content, stringIndexOf + separator.Length, separator) == separator) ||
substring(Content, stringIndexOf - separator.Length, separator) == separator))
{
if (separator.Length == 1 &&
substring(Content, stringIndexOf + separator.Length, separator) != separator)
{
int tempValue = splitCharLength(Content, stringIndexOf, separator);
if (tempValue % 2 == 0)
break;
}
stringIndexOf += separator.Length;
stringIndexOf = Content.IndexOf(separator, stringIndexOf);
}
if (stringIndexOf == -1)
{
stringIndexOf = Content.Length;
}
addString(SplittedString, Content.Substring(0, stringIndexOf), separator);
if (stringIndexOf == Content.Length)
{
break;
}
Content = Content.Substring(stringIndexOf + separator.Length);
if (Content.Contains(separator) == false)
{
addString(SplittedString, Content, separator);
}
}
if (SplittedString.Count == 0)
{
addString(SplittedString, Content, separator);
}
for (int i = 0; i < SplittedString.Count; i++)
{
System.Console.WriteLine(SplittedString[i]);
}
return SplittedString;
}
#region * Helper Functions
private static void addString(List<string> SplittedString, string Content, string separator)
{
SplittedString.Add(Content.Replace(separator + separator, separator));
}
private static int splitCharLength(string Content, int startIndex, string separator)
{
int index = 0;
while (startIndex != 0 && Content[--startIndex].ToString() == separator)
{
index++;
}
return index;
}
private static string substring(string Content, int stringIndexOf, string separator)
{
if (Content.Length <= stringIndexOf)
return "";
if (stringIndexOf <= -1)
return "";
return Content.Substring(stringIndexOf, separator.Length);
}
#endregion
}
}
Hi this is Balaji from India, presently working as a Sr Software Developer in asp.net
using c#.
I am interested in sharing information among peoples, I believe this is one the way to spread and get more ideas.