String Swap-case one-liners (6 ways)
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I did this small project where there are six one-liner anonymous methods that can swap case of a given string of any length. For example "heLLo, woRLd" ==> "HEllO, WOrlD". I used this string as the example to demonstrate how each of the function works. This string "heLLo, woRLd" can simply be replaced with Console.ReadLine() to get user anticipated inputs. I understand one-liners are not readable and maybe not considered good practice, and used variable names are not proper as well. This whole project was for fun. I believe that way I can learn more. My expectation is that someone can tell me if there are more ways to do this case-swapping (of course, has to be one-liner)?. I think I used 3 (kinda) algorithms to do the swapping. Moreover, all of the one-liners are void functions and all the succeeding functions are shorter than the preceding ones.
using System;
using System.Linq;
class x
static void Main(string args)
// one-liner string swapcase
(new Action<string>(delegate (string x) foreach (char i in x) char b = i; b^= char.IsLetter(b) ? (char)32 : (char)0; Console.Write(b); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase2
(new Action<string>(delegate (string x) foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
// one-liner string swapcase3
((Action<string>)(delegate (string x) foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase4
((Action<string>)((x) => foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase5
"heLLo, woRLd".ToList().ForEach(i => Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)));
Console.WriteLine();
//one-liner string swapcase6
"heLLo, woRLd".ToList().ForEach(i => Console.Write(i^= char.IsLetter(i) ? (char)(1 << 5) : (char)0));
Console.ReadLine();
c# strings comparative-review
add a comment |Â
up vote
2
down vote
favorite
I did this small project where there are six one-liner anonymous methods that can swap case of a given string of any length. For example "heLLo, woRLd" ==> "HEllO, WOrlD". I used this string as the example to demonstrate how each of the function works. This string "heLLo, woRLd" can simply be replaced with Console.ReadLine() to get user anticipated inputs. I understand one-liners are not readable and maybe not considered good practice, and used variable names are not proper as well. This whole project was for fun. I believe that way I can learn more. My expectation is that someone can tell me if there are more ways to do this case-swapping (of course, has to be one-liner)?. I think I used 3 (kinda) algorithms to do the swapping. Moreover, all of the one-liners are void functions and all the succeeding functions are shorter than the preceding ones.
using System;
using System.Linq;
class x
static void Main(string args)
// one-liner string swapcase
(new Action<string>(delegate (string x) foreach (char i in x) char b = i; b^= char.IsLetter(b) ? (char)32 : (char)0; Console.Write(b); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase2
(new Action<string>(delegate (string x) foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
// one-liner string swapcase3
((Action<string>)(delegate (string x) foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase4
((Action<string>)((x) => foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase5
"heLLo, woRLd".ToList().ForEach(i => Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)));
Console.WriteLine();
//one-liner string swapcase6
"heLLo, woRLd".ToList().ForEach(i => Console.Write(i^= char.IsLetter(i) ? (char)(1 << 5) : (char)0));
Console.ReadLine();
c# strings comparative-review
You can also loop withforeach (var c in "heLLo, woRLd") ...
.
â Xiaoy312
Jun 26 at 18:50
@Xiaoy312 true. Thanks
â A J
Jun 26 at 18:57
Excluding your last 2 examples, all the rest are not 1 liners, if you consider them 1 line, then you might as well call your entire file 1 liner. Also I wouldn't call a small change of anonymous methods vs lambda, a different way of doing it.
â Denis
Jun 27 at 12:02
@Denis could u care to explain why they r not one-liners? From taking input to printing output was done in a single line. If u donâÂÂt consider lambda a way of doing one-liner than plz come up with a better explanation.
â A J
Jun 27 at 16:03
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I did this small project where there are six one-liner anonymous methods that can swap case of a given string of any length. For example "heLLo, woRLd" ==> "HEllO, WOrlD". I used this string as the example to demonstrate how each of the function works. This string "heLLo, woRLd" can simply be replaced with Console.ReadLine() to get user anticipated inputs. I understand one-liners are not readable and maybe not considered good practice, and used variable names are not proper as well. This whole project was for fun. I believe that way I can learn more. My expectation is that someone can tell me if there are more ways to do this case-swapping (of course, has to be one-liner)?. I think I used 3 (kinda) algorithms to do the swapping. Moreover, all of the one-liners are void functions and all the succeeding functions are shorter than the preceding ones.
using System;
using System.Linq;
class x
static void Main(string args)
// one-liner string swapcase
(new Action<string>(delegate (string x) foreach (char i in x) char b = i; b^= char.IsLetter(b) ? (char)32 : (char)0; Console.Write(b); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase2
(new Action<string>(delegate (string x) foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
// one-liner string swapcase3
((Action<string>)(delegate (string x) foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase4
((Action<string>)((x) => foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase5
"heLLo, woRLd".ToList().ForEach(i => Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)));
Console.WriteLine();
//one-liner string swapcase6
"heLLo, woRLd".ToList().ForEach(i => Console.Write(i^= char.IsLetter(i) ? (char)(1 << 5) : (char)0));
Console.ReadLine();
c# strings comparative-review
I did this small project where there are six one-liner anonymous methods that can swap case of a given string of any length. For example "heLLo, woRLd" ==> "HEllO, WOrlD". I used this string as the example to demonstrate how each of the function works. This string "heLLo, woRLd" can simply be replaced with Console.ReadLine() to get user anticipated inputs. I understand one-liners are not readable and maybe not considered good practice, and used variable names are not proper as well. This whole project was for fun. I believe that way I can learn more. My expectation is that someone can tell me if there are more ways to do this case-swapping (of course, has to be one-liner)?. I think I used 3 (kinda) algorithms to do the swapping. Moreover, all of the one-liners are void functions and all the succeeding functions are shorter than the preceding ones.
using System;
using System.Linq;
class x
static void Main(string args)
// one-liner string swapcase
(new Action<string>(delegate (string x) foreach (char i in x) char b = i; b^= char.IsLetter(b) ? (char)32 : (char)0; Console.Write(b); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase2
(new Action<string>(delegate (string x) foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
// one-liner string swapcase3
((Action<string>)(delegate (string x) foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase4
((Action<string>)((x) => foreach (char i in x) Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)); ))("heLLo, woRLd");
Console.WriteLine();
//one-liner string swapcase5
"heLLo, woRLd".ToList().ForEach(i => Console.Write(char.IsUpper(i) ? char.ToLower(i) : char.ToUpper(i)));
Console.WriteLine();
//one-liner string swapcase6
"heLLo, woRLd".ToList().ForEach(i => Console.Write(i^= char.IsLetter(i) ? (char)(1 << 5) : (char)0));
Console.ReadLine();
c# strings comparative-review
edited Jun 26 at 19:27
200_success
123k14143399
123k14143399
asked Jun 26 at 17:29
A J
1639
1639
You can also loop withforeach (var c in "heLLo, woRLd") ...
.
â Xiaoy312
Jun 26 at 18:50
@Xiaoy312 true. Thanks
â A J
Jun 26 at 18:57
Excluding your last 2 examples, all the rest are not 1 liners, if you consider them 1 line, then you might as well call your entire file 1 liner. Also I wouldn't call a small change of anonymous methods vs lambda, a different way of doing it.
â Denis
Jun 27 at 12:02
@Denis could u care to explain why they r not one-liners? From taking input to printing output was done in a single line. If u donâÂÂt consider lambda a way of doing one-liner than plz come up with a better explanation.
â A J
Jun 27 at 16:03
add a comment |Â
You can also loop withforeach (var c in "heLLo, woRLd") ...
.
â Xiaoy312
Jun 26 at 18:50
@Xiaoy312 true. Thanks
â A J
Jun 26 at 18:57
Excluding your last 2 examples, all the rest are not 1 liners, if you consider them 1 line, then you might as well call your entire file 1 liner. Also I wouldn't call a small change of anonymous methods vs lambda, a different way of doing it.
â Denis
Jun 27 at 12:02
@Denis could u care to explain why they r not one-liners? From taking input to printing output was done in a single line. If u donâÂÂt consider lambda a way of doing one-liner than plz come up with a better explanation.
â A J
Jun 27 at 16:03
You can also loop with
foreach (var c in "heLLo, woRLd") ...
.â Xiaoy312
Jun 26 at 18:50
You can also loop with
foreach (var c in "heLLo, woRLd") ...
.â Xiaoy312
Jun 26 at 18:50
@Xiaoy312 true. Thanks
â A J
Jun 26 at 18:57
@Xiaoy312 true. Thanks
â A J
Jun 26 at 18:57
Excluding your last 2 examples, all the rest are not 1 liners, if you consider them 1 line, then you might as well call your entire file 1 liner. Also I wouldn't call a small change of anonymous methods vs lambda, a different way of doing it.
â Denis
Jun 27 at 12:02
Excluding your last 2 examples, all the rest are not 1 liners, if you consider them 1 line, then you might as well call your entire file 1 liner. Also I wouldn't call a small change of anonymous methods vs lambda, a different way of doing it.
â Denis
Jun 27 at 12:02
@Denis could u care to explain why they r not one-liners? From taking input to printing output was done in a single line. If u donâÂÂt consider lambda a way of doing one-liner than plz come up with a better explanation.
â A J
Jun 27 at 16:03
@Denis could u care to explain why they r not one-liners? From taking input to printing output was done in a single line. If u donâÂÂt consider lambda a way of doing one-liner than plz come up with a better explanation.
â A J
Jun 27 at 16:03
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
9
down vote
I find your code amusing but since this is not Code Golf but Code Review I'll review it from our perspective this is, as clean-code first.
Even if you try to write compact code you should not use magic exressions and sacrifice readability. Instead encapsulate them in (local) function or use proper constants. You also should not use unrelated variable names like here i
for char
where c
would be more appropriate. Alternatively you can also use the Aggregate
to loop over the string and collect the results in a StringBuilder
. The (char)0
can be replaced with the much prettier default
.
const char caseOffset = (char)(1 << 5);
char SwapCase(char c) => c ^= (char.IsLetter(c) ? caseOffset : default);
var result =
"heLLo, woRLd"
.Aggregate(
new StringBuilder(),
(b, c) => b.Append(SwapCase(c))
).ToString();
One more option would be to use Select
and new string
and feed it with the new result.
var result = new string("heLLo, woRLd".Select(SwapCase).ToArray())
The advantage of Select
is that you don't need the =>
lambda anymore.
Again, our definitions of code-golf will have to differ! You can have a +1 if you add a brief comment to this fine answer about Unicode; there isn't much more I'd say that would warrant a full answer ;)
â VisualMelon
Jun 26 at 19:14
Thanks, I mentioned before that it was for fun. So, I didnâÂÂt consider readability. Your solutions are interesting, but I was expecting to have one-liners (from taking input to printing output in one line. But I appreciate your feedback.
â A J
Jun 26 at 19:15
@VisualMelon I quess I'm not going to earn that +1 :P as I have no idea how to deal with unicode exactly (right now) let's wait for Adrianno Repetti - he's the unicode expert - I know that it won't work for every language and to be honest... I naively assumed the english alphabet. Would I have to work with other languages I'd probably go withToUpper/Lower
.
â t3chb0t
Jun 26 at 19:22
1
@t3chb0t oh, well have it anyway then! I'll await Adrianno Repetti to tell us how it should be done properly instead of making something up :P
â VisualMelon
Jun 26 at 19:24
SwapCase
should be an extension method onchar
and should useIsUpper
,ToUpper
, andToLower
to get the job done.
â David Conrad
Jun 26 at 20:55
add a comment |Â
up vote
0
down vote
First, I want to thank @t3chb0t for giving such concrete feedback; thanks to this person I was able to have more ways to do the swapping of this above mentioned string. I am posting this as an answer since none else contributed any actual one-liners to my question.
//one-liner string swapcase7 ***idea from @t3chb0t***
Console.WriteLine(new string("heLLo, woRLd".Select(i => i^= char.IsLetter(i) ? (char)(1 << 5) : default).ToArray()));
//one-liner string swapcase8 ***idea from @t3chb0t***
Console.WriteLine("heLLo, woRLd".Aggregate(new StringBuilder(), (b, o) => b.Append(((Func<char, char>)((a) => a ^= char.IsLetter(a) ? (char)(1 << 5) : default; return a; ))(o))).ToString());
//one-liner string swapcase9 ***my own understanding of LINQ***
Console.WriteLine(new string((from i in "heLLo, woRLd" select ((Func<char, char>)((a) => a ^= char.IsLetter(a) ? (char)(1 << 5) : default; return a; ))(i)).ToArray()));
1
Since OP apparently doesn't need a newline in the one-liner:Console.Write
accepts achar
, so you save theLine
and don't need thenew string(char)
â Corak
Jun 27 at 8:03
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
I find your code amusing but since this is not Code Golf but Code Review I'll review it from our perspective this is, as clean-code first.
Even if you try to write compact code you should not use magic exressions and sacrifice readability. Instead encapsulate them in (local) function or use proper constants. You also should not use unrelated variable names like here i
for char
where c
would be more appropriate. Alternatively you can also use the Aggregate
to loop over the string and collect the results in a StringBuilder
. The (char)0
can be replaced with the much prettier default
.
const char caseOffset = (char)(1 << 5);
char SwapCase(char c) => c ^= (char.IsLetter(c) ? caseOffset : default);
var result =
"heLLo, woRLd"
.Aggregate(
new StringBuilder(),
(b, c) => b.Append(SwapCase(c))
).ToString();
One more option would be to use Select
and new string
and feed it with the new result.
var result = new string("heLLo, woRLd".Select(SwapCase).ToArray())
The advantage of Select
is that you don't need the =>
lambda anymore.
Again, our definitions of code-golf will have to differ! You can have a +1 if you add a brief comment to this fine answer about Unicode; there isn't much more I'd say that would warrant a full answer ;)
â VisualMelon
Jun 26 at 19:14
Thanks, I mentioned before that it was for fun. So, I didnâÂÂt consider readability. Your solutions are interesting, but I was expecting to have one-liners (from taking input to printing output in one line. But I appreciate your feedback.
â A J
Jun 26 at 19:15
@VisualMelon I quess I'm not going to earn that +1 :P as I have no idea how to deal with unicode exactly (right now) let's wait for Adrianno Repetti - he's the unicode expert - I know that it won't work for every language and to be honest... I naively assumed the english alphabet. Would I have to work with other languages I'd probably go withToUpper/Lower
.
â t3chb0t
Jun 26 at 19:22
1
@t3chb0t oh, well have it anyway then! I'll await Adrianno Repetti to tell us how it should be done properly instead of making something up :P
â VisualMelon
Jun 26 at 19:24
SwapCase
should be an extension method onchar
and should useIsUpper
,ToUpper
, andToLower
to get the job done.
â David Conrad
Jun 26 at 20:55
add a comment |Â
up vote
9
down vote
I find your code amusing but since this is not Code Golf but Code Review I'll review it from our perspective this is, as clean-code first.
Even if you try to write compact code you should not use magic exressions and sacrifice readability. Instead encapsulate them in (local) function or use proper constants. You also should not use unrelated variable names like here i
for char
where c
would be more appropriate. Alternatively you can also use the Aggregate
to loop over the string and collect the results in a StringBuilder
. The (char)0
can be replaced with the much prettier default
.
const char caseOffset = (char)(1 << 5);
char SwapCase(char c) => c ^= (char.IsLetter(c) ? caseOffset : default);
var result =
"heLLo, woRLd"
.Aggregate(
new StringBuilder(),
(b, c) => b.Append(SwapCase(c))
).ToString();
One more option would be to use Select
and new string
and feed it with the new result.
var result = new string("heLLo, woRLd".Select(SwapCase).ToArray())
The advantage of Select
is that you don't need the =>
lambda anymore.
Again, our definitions of code-golf will have to differ! You can have a +1 if you add a brief comment to this fine answer about Unicode; there isn't much more I'd say that would warrant a full answer ;)
â VisualMelon
Jun 26 at 19:14
Thanks, I mentioned before that it was for fun. So, I didnâÂÂt consider readability. Your solutions are interesting, but I was expecting to have one-liners (from taking input to printing output in one line. But I appreciate your feedback.
â A J
Jun 26 at 19:15
@VisualMelon I quess I'm not going to earn that +1 :P as I have no idea how to deal with unicode exactly (right now) let's wait for Adrianno Repetti - he's the unicode expert - I know that it won't work for every language and to be honest... I naively assumed the english alphabet. Would I have to work with other languages I'd probably go withToUpper/Lower
.
â t3chb0t
Jun 26 at 19:22
1
@t3chb0t oh, well have it anyway then! I'll await Adrianno Repetti to tell us how it should be done properly instead of making something up :P
â VisualMelon
Jun 26 at 19:24
SwapCase
should be an extension method onchar
and should useIsUpper
,ToUpper
, andToLower
to get the job done.
â David Conrad
Jun 26 at 20:55
add a comment |Â
up vote
9
down vote
up vote
9
down vote
I find your code amusing but since this is not Code Golf but Code Review I'll review it from our perspective this is, as clean-code first.
Even if you try to write compact code you should not use magic exressions and sacrifice readability. Instead encapsulate them in (local) function or use proper constants. You also should not use unrelated variable names like here i
for char
where c
would be more appropriate. Alternatively you can also use the Aggregate
to loop over the string and collect the results in a StringBuilder
. The (char)0
can be replaced with the much prettier default
.
const char caseOffset = (char)(1 << 5);
char SwapCase(char c) => c ^= (char.IsLetter(c) ? caseOffset : default);
var result =
"heLLo, woRLd"
.Aggregate(
new StringBuilder(),
(b, c) => b.Append(SwapCase(c))
).ToString();
One more option would be to use Select
and new string
and feed it with the new result.
var result = new string("heLLo, woRLd".Select(SwapCase).ToArray())
The advantage of Select
is that you don't need the =>
lambda anymore.
I find your code amusing but since this is not Code Golf but Code Review I'll review it from our perspective this is, as clean-code first.
Even if you try to write compact code you should not use magic exressions and sacrifice readability. Instead encapsulate them in (local) function or use proper constants. You also should not use unrelated variable names like here i
for char
where c
would be more appropriate. Alternatively you can also use the Aggregate
to loop over the string and collect the results in a StringBuilder
. The (char)0
can be replaced with the much prettier default
.
const char caseOffset = (char)(1 << 5);
char SwapCase(char c) => c ^= (char.IsLetter(c) ? caseOffset : default);
var result =
"heLLo, woRLd"
.Aggregate(
new StringBuilder(),
(b, c) => b.Append(SwapCase(c))
).ToString();
One more option would be to use Select
and new string
and feed it with the new result.
var result = new string("heLLo, woRLd".Select(SwapCase).ToArray())
The advantage of Select
is that you don't need the =>
lambda anymore.
edited Jun 26 at 19:15
answered Jun 26 at 19:08
t3chb0t
31.9k54095
31.9k54095
Again, our definitions of code-golf will have to differ! You can have a +1 if you add a brief comment to this fine answer about Unicode; there isn't much more I'd say that would warrant a full answer ;)
â VisualMelon
Jun 26 at 19:14
Thanks, I mentioned before that it was for fun. So, I didnâÂÂt consider readability. Your solutions are interesting, but I was expecting to have one-liners (from taking input to printing output in one line. But I appreciate your feedback.
â A J
Jun 26 at 19:15
@VisualMelon I quess I'm not going to earn that +1 :P as I have no idea how to deal with unicode exactly (right now) let's wait for Adrianno Repetti - he's the unicode expert - I know that it won't work for every language and to be honest... I naively assumed the english alphabet. Would I have to work with other languages I'd probably go withToUpper/Lower
.
â t3chb0t
Jun 26 at 19:22
1
@t3chb0t oh, well have it anyway then! I'll await Adrianno Repetti to tell us how it should be done properly instead of making something up :P
â VisualMelon
Jun 26 at 19:24
SwapCase
should be an extension method onchar
and should useIsUpper
,ToUpper
, andToLower
to get the job done.
â David Conrad
Jun 26 at 20:55
add a comment |Â
Again, our definitions of code-golf will have to differ! You can have a +1 if you add a brief comment to this fine answer about Unicode; there isn't much more I'd say that would warrant a full answer ;)
â VisualMelon
Jun 26 at 19:14
Thanks, I mentioned before that it was for fun. So, I didnâÂÂt consider readability. Your solutions are interesting, but I was expecting to have one-liners (from taking input to printing output in one line. But I appreciate your feedback.
â A J
Jun 26 at 19:15
@VisualMelon I quess I'm not going to earn that +1 :P as I have no idea how to deal with unicode exactly (right now) let's wait for Adrianno Repetti - he's the unicode expert - I know that it won't work for every language and to be honest... I naively assumed the english alphabet. Would I have to work with other languages I'd probably go withToUpper/Lower
.
â t3chb0t
Jun 26 at 19:22
1
@t3chb0t oh, well have it anyway then! I'll await Adrianno Repetti to tell us how it should be done properly instead of making something up :P
â VisualMelon
Jun 26 at 19:24
SwapCase
should be an extension method onchar
and should useIsUpper
,ToUpper
, andToLower
to get the job done.
â David Conrad
Jun 26 at 20:55
Again, our definitions of code-golf will have to differ! You can have a +1 if you add a brief comment to this fine answer about Unicode; there isn't much more I'd say that would warrant a full answer ;)
â VisualMelon
Jun 26 at 19:14
Again, our definitions of code-golf will have to differ! You can have a +1 if you add a brief comment to this fine answer about Unicode; there isn't much more I'd say that would warrant a full answer ;)
â VisualMelon
Jun 26 at 19:14
Thanks, I mentioned before that it was for fun. So, I didnâÂÂt consider readability. Your solutions are interesting, but I was expecting to have one-liners (from taking input to printing output in one line. But I appreciate your feedback.
â A J
Jun 26 at 19:15
Thanks, I mentioned before that it was for fun. So, I didnâÂÂt consider readability. Your solutions are interesting, but I was expecting to have one-liners (from taking input to printing output in one line. But I appreciate your feedback.
â A J
Jun 26 at 19:15
@VisualMelon I quess I'm not going to earn that +1 :P as I have no idea how to deal with unicode exactly (right now) let's wait for Adrianno Repetti - he's the unicode expert - I know that it won't work for every language and to be honest... I naively assumed the english alphabet. Would I have to work with other languages I'd probably go with
ToUpper/Lower
.â t3chb0t
Jun 26 at 19:22
@VisualMelon I quess I'm not going to earn that +1 :P as I have no idea how to deal with unicode exactly (right now) let's wait for Adrianno Repetti - he's the unicode expert - I know that it won't work for every language and to be honest... I naively assumed the english alphabet. Would I have to work with other languages I'd probably go with
ToUpper/Lower
.â t3chb0t
Jun 26 at 19:22
1
1
@t3chb0t oh, well have it anyway then! I'll await Adrianno Repetti to tell us how it should be done properly instead of making something up :P
â VisualMelon
Jun 26 at 19:24
@t3chb0t oh, well have it anyway then! I'll await Adrianno Repetti to tell us how it should be done properly instead of making something up :P
â VisualMelon
Jun 26 at 19:24
SwapCase
should be an extension method on char
and should use IsUpper
, ToUpper
, and ToLower
to get the job done.â David Conrad
Jun 26 at 20:55
SwapCase
should be an extension method on char
and should use IsUpper
, ToUpper
, and ToLower
to get the job done.â David Conrad
Jun 26 at 20:55
add a comment |Â
up vote
0
down vote
First, I want to thank @t3chb0t for giving such concrete feedback; thanks to this person I was able to have more ways to do the swapping of this above mentioned string. I am posting this as an answer since none else contributed any actual one-liners to my question.
//one-liner string swapcase7 ***idea from @t3chb0t***
Console.WriteLine(new string("heLLo, woRLd".Select(i => i^= char.IsLetter(i) ? (char)(1 << 5) : default).ToArray()));
//one-liner string swapcase8 ***idea from @t3chb0t***
Console.WriteLine("heLLo, woRLd".Aggregate(new StringBuilder(), (b, o) => b.Append(((Func<char, char>)((a) => a ^= char.IsLetter(a) ? (char)(1 << 5) : default; return a; ))(o))).ToString());
//one-liner string swapcase9 ***my own understanding of LINQ***
Console.WriteLine(new string((from i in "heLLo, woRLd" select ((Func<char, char>)((a) => a ^= char.IsLetter(a) ? (char)(1 << 5) : default; return a; ))(i)).ToArray()));
1
Since OP apparently doesn't need a newline in the one-liner:Console.Write
accepts achar
, so you save theLine
and don't need thenew string(char)
â Corak
Jun 27 at 8:03
add a comment |Â
up vote
0
down vote
First, I want to thank @t3chb0t for giving such concrete feedback; thanks to this person I was able to have more ways to do the swapping of this above mentioned string. I am posting this as an answer since none else contributed any actual one-liners to my question.
//one-liner string swapcase7 ***idea from @t3chb0t***
Console.WriteLine(new string("heLLo, woRLd".Select(i => i^= char.IsLetter(i) ? (char)(1 << 5) : default).ToArray()));
//one-liner string swapcase8 ***idea from @t3chb0t***
Console.WriteLine("heLLo, woRLd".Aggregate(new StringBuilder(), (b, o) => b.Append(((Func<char, char>)((a) => a ^= char.IsLetter(a) ? (char)(1 << 5) : default; return a; ))(o))).ToString());
//one-liner string swapcase9 ***my own understanding of LINQ***
Console.WriteLine(new string((from i in "heLLo, woRLd" select ((Func<char, char>)((a) => a ^= char.IsLetter(a) ? (char)(1 << 5) : default; return a; ))(i)).ToArray()));
1
Since OP apparently doesn't need a newline in the one-liner:Console.Write
accepts achar
, so you save theLine
and don't need thenew string(char)
â Corak
Jun 27 at 8:03
add a comment |Â
up vote
0
down vote
up vote
0
down vote
First, I want to thank @t3chb0t for giving such concrete feedback; thanks to this person I was able to have more ways to do the swapping of this above mentioned string. I am posting this as an answer since none else contributed any actual one-liners to my question.
//one-liner string swapcase7 ***idea from @t3chb0t***
Console.WriteLine(new string("heLLo, woRLd".Select(i => i^= char.IsLetter(i) ? (char)(1 << 5) : default).ToArray()));
//one-liner string swapcase8 ***idea from @t3chb0t***
Console.WriteLine("heLLo, woRLd".Aggregate(new StringBuilder(), (b, o) => b.Append(((Func<char, char>)((a) => a ^= char.IsLetter(a) ? (char)(1 << 5) : default; return a; ))(o))).ToString());
//one-liner string swapcase9 ***my own understanding of LINQ***
Console.WriteLine(new string((from i in "heLLo, woRLd" select ((Func<char, char>)((a) => a ^= char.IsLetter(a) ? (char)(1 << 5) : default; return a; ))(i)).ToArray()));
First, I want to thank @t3chb0t for giving such concrete feedback; thanks to this person I was able to have more ways to do the swapping of this above mentioned string. I am posting this as an answer since none else contributed any actual one-liners to my question.
//one-liner string swapcase7 ***idea from @t3chb0t***
Console.WriteLine(new string("heLLo, woRLd".Select(i => i^= char.IsLetter(i) ? (char)(1 << 5) : default).ToArray()));
//one-liner string swapcase8 ***idea from @t3chb0t***
Console.WriteLine("heLLo, woRLd".Aggregate(new StringBuilder(), (b, o) => b.Append(((Func<char, char>)((a) => a ^= char.IsLetter(a) ? (char)(1 << 5) : default; return a; ))(o))).ToString());
//one-liner string swapcase9 ***my own understanding of LINQ***
Console.WriteLine(new string((from i in "heLLo, woRLd" select ((Func<char, char>)((a) => a ^= char.IsLetter(a) ? (char)(1 << 5) : default; return a; ))(i)).ToArray()));
edited Jun 26 at 22:27
answered Jun 26 at 22:15
A J
1639
1639
1
Since OP apparently doesn't need a newline in the one-liner:Console.Write
accepts achar
, so you save theLine
and don't need thenew string(char)
â Corak
Jun 27 at 8:03
add a comment |Â
1
Since OP apparently doesn't need a newline in the one-liner:Console.Write
accepts achar
, so you save theLine
and don't need thenew string(char)
â Corak
Jun 27 at 8:03
1
1
Since OP apparently doesn't need a newline in the one-liner:
Console.Write
accepts a char
, so you save the Line
and don't need the new string(char)
â Corak
Jun 27 at 8:03
Since OP apparently doesn't need a newline in the one-liner:
Console.Write
accepts a char
, so you save the Line
and don't need the new string(char)
â Corak
Jun 27 at 8:03
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197295%2fstring-swap-case-one-liners-6-ways%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You can also loop with
foreach (var c in "heLLo, woRLd") ...
.â Xiaoy312
Jun 26 at 18:50
@Xiaoy312 true. Thanks
â A J
Jun 26 at 18:57
Excluding your last 2 examples, all the rest are not 1 liners, if you consider them 1 line, then you might as well call your entire file 1 liner. Also I wouldn't call a small change of anonymous methods vs lambda, a different way of doing it.
â Denis
Jun 27 at 12:02
@Denis could u care to explain why they r not one-liners? From taking input to printing output was done in a single line. If u donâÂÂt consider lambda a way of doing one-liner than plz come up with a better explanation.
â A J
Jun 27 at 16:03