'I am struggling with trying to use the Where() method in C# to turn a string into its camel case, code does not do anything
as mentioned in the title I am trying to turn a given string with spaces in between words into a camel case format, for example: "camel case" becomes "camelCase".
My method is to try and turn the string into an array of characters, and then check if the previous character was a space, but I am stuck on the part where I am using a Where() method in order to check every character:
str = new string(str.Where(x => Array.IndexOf(str.ToCharArray(), x) - 1 != ' ').ToArray());
This just returns the original string without doing anything.
Any help would be appreciated, and maybe you could suggest a better way of doing it too, after all, we're all in this together right?
Solution 1:[1]
Here's my version with a linq select
public static string ToCamelCase(string value)
{
int n = 0;
return string.Join("",
value.ToLower()
.Split(' ')
.Select(t => string.IsNullOrEmpty(t) ? ""
: n++ == 0
? t
: $"{t.Substring(0, 1).ToUpper()}{t.Substring(1)}"
);
}
Solution 2:[2]
Unless you need to do it without regular expressions I would just use that.
Here's one way:
Regex
.Replace(str, @"\s+([^\s])", ma => ma.Groups[1].Value.ToUpperInvariant())
.TrimEnd()
This will replace any sequence of one or more whitespace characters that are followed by a non-whitespace character with the uppercase version of that non-whitespace character.
Essentially it will remove all whitespace (except for at the very end, thus the .TrimEnd()
method), and it will try to convert the first character following each sequence of whitespace to uppercase.
Example code:
string input = "locate the next person of age 50 or above";
string output = Regex
.Replace(input, @"\s+([^\s])", ma => ma.Groups[1].Value.ToUpperInvariant())
.Trim();
Console.WriteLine(output);
Output:
locateTheNextPersonOfAge50OrAbove
Solution 3:[3]
I would use a simple loop.
public static string ToCamelCase(this string value)
{
string result = string.Empty;
bool nextUpper = false;
for (int i = 0; i < value.Length; i++)
{
if (value[i] == ' ')
nextUpper = true;
else
{
if (nextUpper)
result += value[i].ToString().ToUpper();
else
result += value[i].ToString().ToLower();
nextUpper = false;
}
}
return result;
}
Solution 4:[4]
It's because there are 2 c
s in your string, so Array.IndexOf() will always return position of first c
. Maybe some LINQ magician can fix this with proper LINQ, but if you don't mind using other methods, a StringBuilder
and a foreach-loop through str
can achieve the quest.
Something like:
string str = "camel case";
StringBuilder sb = new StringBuilder();
bool pickedSpace = false;
foreach (char c in str)
{
if (c == ' ')
pickedSpace = true;
else if (pickedSpace) {
sb.Append(Char.ToUpper(c));
pickedSpace = false;
} else {
sb.Append(c);
}
}
Console.WriteLine(sb.ToString());
You can also check if the last element in the StringBuilder
is space, then remove it and ToUpper()
next char to join instead of using a boolean, for less overhead.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Lasse V. Karlsen |
Solution 3 | Steve Harris |
Solution 4 | Xiang Wei Huang |