How to go from string abc to string a|b|c

  • Thread starter Thread starter Webgour
  • Start date Start date
Webgour,

I would cycle through the characters, and then append the items in a new
StringBuilder, like so:

public static string DelimitString(string value, string delimiter)
{
// If value is null or has zero length, then return value.
// If delimiter is null or has zero length, then return value.
if (value == null || value.Length == 0 || delimiter == null ||
delimiter.Length == 0)
// Get out.
return value;

// Create a string builder, allocate the space equal the length of the
delimiter plus one times
// the number of characters in the string.
StringBuilder pobjBuilder = new StringBuilder((delimiter.Length + 1) *
value.Length;

// Cycle through the characters.
foreach (char pchrChar in value)
{
// Append the character.
pobjBuilder.Append(pchrChar);

// Append the delimiter.
pobjBuilder.Append(delimiter);
}

// Return the string.
return pobjBuilder.ToString();
}

Hope this helps.
 
The character array version is probably going to be significantly faster. We
recently
found this out when working with string builders in a string reversal challenge
I posted
on my blog.

http://weblogs.asp.net/justin_rogers/archive/2004/06/09/152343.aspx
http://weblogs.asp.net/justin_rogers/archive/2004/06/10/153175.aspx

public static string DelimitString(string value, string delimiter) {
char[] work = new char[value.Length + (delimiter.Length *
(value.Length - 1))];
for(int i = 0, j = 0; i < value.Length - 1; i++, j++) {
work[j] = value;
for(int k = 0; k < delimiter.Length; k++) {
work[++j] = delimiter[k];
}
}
work[work.Length - 1] = value[value.Length - 1];
return new string(work);
}


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

Nicholas Paldino said:
Webgour,

I would cycle through the characters, and then append the items in a new
StringBuilder, like so:

public static string DelimitString(string value, string delimiter)
{
// If value is null or has zero length, then return value.
// If delimiter is null or has zero length, then return value.
if (value == null || value.Length == 0 || delimiter == null ||
delimiter.Length == 0)
// Get out.
return value;

// Create a string builder, allocate the space equal the length of the
delimiter plus one times
// the number of characters in the string.
StringBuilder pobjBuilder = new StringBuilder((delimiter.Length + 1) *
value.Length;

// Cycle through the characters.
foreach (char pchrChar in value)
{
// Append the character.
pobjBuilder.Append(pchrChar);

// Append the delimiter.
pobjBuilder.Append(delimiter);
}

// Return the string.
return pobjBuilder.ToString();
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Webgour said:
How to go from string "abc" to string "a|b|c"?
 
A simple solution...don't know whether this is what you r looking for.
Dim first As String = "abc"
Dim second As String

For index As Integer = 0 To first.Length - 1
second = second & first.Chars(index) & "|"
Next
Dim thrid = second.Substring(0, second.Length - 1)
 
Hello!
A simple solution...don't know whether this is what you r looking for.

Which is not recommendable because of the string operations presented. It's
best to stick with stringbuilders or the array approach.
 
Back
Top