473,379 Members | 1,190 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

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


Nov 16 '05 #1
6 2168
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]
- mv*@spam.guard.caspershouse.com

"Webgour" <ol****@gsmzoo.com> wrote in message
news:ei**************@tk2msftngp13.phx.gbl...
How to go from string "abc" to string "a|b|c"?

Nov 16 '05 #2
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...09/152343.aspx
http://weblogs.asp.net/justin_rogers...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[i];
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u5**************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Webgour" <ol****@gsmzoo.com> wrote in message
news:ei**************@tk2msftngp13.phx.gbl...
How to go from string "abc" to string "a|b|c"?


Nov 16 '05 #3
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)

--
Rakesh Rajan
"Webgour" wrote:
How to go from string "abc" to string "a|b|c"?


Nov 16 '05 #4

"Webgour" <ol****@gsmzoo.com> wrote in message
news:ei**************@tk2msftngp13.phx.gbl...
How to go from string "abc" to string "a|b|c"?


string.Join( "|", "abc".ToCharArray() );

Nov 16 '05 #5
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.

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #6
StringBuilder's are inherently poor when appending single characters due to
some thread checking in both the indexer for the StringBuilder and the Append
method. You can get details in the comments of the following blog posting:

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

"Anders Borum" <a@b.dk> wrote in message
news:uz**************@TK2MSFTNGP12.phx.gbl...
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.

--
venlig hilsen / with regards
anders borum
--

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.