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.
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.
// 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"
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)
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(", ", "\" + \"");
var results = from s in string.Split("risk management, portfolio management, investment planning", new char[] { ',' })
select s.Trim();
HTH, Kent
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.
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, ", ");
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
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);
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());