views:

1031

answers:

9

Hi,

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.

+18  A: 

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"
Koistya Navin
thanks a lot...this works greatttttttttttt!:-)
Jimmy
am now getting the output as:"\"tax accounting\" + \"assurance services\" + \"tax audit\" + \"internal audit\" + \"lean accounting\" + \"cost accounting\"...can i get rid of the back slashes over here?
Jimmy
back slashes could be in a quick property view, i don't think they exist in an actual value
Koistya Navin
+6  A: 

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)

Andrew Flanagan
That works, but he'll have leading spaces at the beginning of every item after the first.
John Feminella
Thanks... Updated post
Andrew Flanagan
+2  A: 

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(", ", "\" + \"");
Jhonny D. Cano -Leftware-
Ummm +1? Amazing how the only correct answer gets downvoted...
John Rasch
The do not like "smarty pants" here.
alex
(based on the ambiguity of the question of course)
John Rasch
I'm, amazed of that also, tx for upvoting
Jhonny D. Cano -Leftware-
Well, I shouldn't say "only"... since we can't read his mind :) - but based on the specifications this is "most likely" what he wants, not a "smarty pants" answer...
John Rasch
Hmm, this solution makes assumption that leading and trailing quotes are parts of input. This is very unusual.
alex
True, but since his input and output onto different lines we can only assume that he wants those strings manipulated verbatim. I think it's less of a stretch than assuming he wants an array of strings. Again, since there are no details no one can really know for sure
John Rasch
Sorry, this is a kind of ambiguous answer for an ambiguous question
Jhonny D. Cano -Leftware-
i've got the answer from Koistya Navin .NET above.thanks everyone for your inputs.FYI,i did not care much about whitespaces getting trimmed or not.my main concern was not being able to add the "+" character literally in the output string which Koistya's solution achieves using Aggregate function....
Jimmy
+3  A: 
var results = from s in string.Split("risk management, portfolio management, investment planning", new char[] { ',' })
              select s.Trim();

HTH, Kent

Kent Boogaart
+1  A: 

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.

Adam Robinson
+3  A: 

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, ", ");
John Feminella
+1  A: 

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
Fraser
+2  A: 

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);
bruno conde
A: 

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());

Hawker