Jimmy
Jimmy

Reputation: 2106

String concatenation using C#

I've a input string:

"risk management, portfolio management, investment planning"

How do I convert this string into:

"risk management" + "portfolio management" + "investment planning"

Thanks.

Upvotes: 0

Views: 2161

Answers (9)

Kent Boogaart
Kent Boogaart

Reputation: 178810

var results = from s in string.Split("risk management, portfolio management, investment planning", new char[] { ',' })
              select s.Trim();

Upvotes: 3

Konstantin Tarkus
Konstantin Tarkus

Reputation: 38428

Split and Trim

 // include linq library like this:
 // using System.Linq;
 // then

"test1, test2".Split(',').Select(o => o.Trim());

or

"test1, test2".Split(',').Select(o => o.Trim()).ToArray(); // returns array

and

"test1, test2".Split(',').Select(o => "\"" + o.Trim() + "\"")
.Aggregate((s1, s2) => s1 + " + " + s2);

// returns a string: "test1" + "test2"

Upvotes: 18

ahawker
ahawker

Reputation: 3384

Reply to Jhonny D. Cano (Sorry, don't have 50 rep for a comment.)

Your first recommendation

string[] array = inputString.Split(", ");

Doesn't work because you can't split on a string. The closest possible overload is a char[], so you would have to write it as...

string[] array = inputString.Split(", ".ToCharArray());

Upvotes: 0

Jhonny D. Cano -Leftware-
Jhonny D. Cano -Leftware-

Reputation: 18013

If you want to split the input, you can use string.Split, using comma as a delimiter or , even better ", " for taking into account the space after comma,

string[] array = inputString.Split(", ");

However, you can be wanting to replace the comma inside the string for a plus sign, this is how you could be achieving that also:

inputString = inputString.Replace(", ", "\" + \"");

Upvotes: 2

bruno conde
bruno conde

Reputation: 48255

Your question is not clear on whether you want to replace the ',' for '+' or just a simple split.

Here are the 2 possibilities:

string s = "risk management, portfolio management, investment planning";

string transformedString = s.Replace(", ", "\" + \"");

string[] parts = s.Split(new [] {", "}, StringSplitOptions.None);

Upvotes: 2

Andrew Flanagan
Andrew Flanagan

Reputation: 4277

Use the Split() method:

string[] phrases = s.Split(',');

Now you have a string array of each comma separated value.

To remove the spaces, use the Trim() method on each string (thanks John Feminella)

Upvotes: 6

Fraser
Fraser

Reputation: 17094

It is not very clear what you mean. If you need to access the CSV values then this will output each value separately...

string input = "risk management, portfolio management, investment planning";
string[] words = text.Split(new Char[] {','});
foreach(string word in words)
{
  Console.WriteLine(word.Trim());
}
//risk management
//portfolio management
//investment planning

Upvotes: 1

John Feminella
John Feminella

Reputation: 311755

You can't use String.Split() in your case because you have a comma, then a space. So your strings will look like { "risk management", " portfolio management", " investment planning" }. Instead, use Regex.Split:

string[] investmentServices = Regex.Split(inputString, ", ");

Upvotes: 3

Adam Robinson
Adam Robinson

Reputation: 185703

It actually looks like you're trying to perform a split, rather than concatenation.

If you're looking to take that input string and convert it into three strings containing "risk management", "portfolio management", and "investment planning", then use string.Split(inputString, ','), then trim each string from the resulting array when you use it.

Upvotes: 1

Related Questions