String Swap-case one-liners (6 ways)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite
1












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








share|improve this question





















  • 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
















up vote
2
down vote

favorite
1












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








share|improve this question





















  • 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












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





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








share|improve this question













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










share|improve this question












share|improve this question




share|improve this question








edited Jun 26 at 19:27









200_success

123k14143399




123k14143399









asked Jun 26 at 17:29









A J

1639




1639











  • 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
















  • 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















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










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.






share|improve this answer























  • 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 with ToUpper/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 on char and should use IsUpper, ToUpper, and ToLower to get the job done.
    – David Conrad
    Jun 26 at 20:55

















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





share|improve this answer



















  • 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











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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.






share|improve this answer























  • 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 with ToUpper/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 on char and should use IsUpper, ToUpper, and ToLower to get the job done.
    – David Conrad
    Jun 26 at 20:55














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.






share|improve this answer























  • 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 with ToUpper/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 on char and should use IsUpper, ToUpper, and ToLower to get the job done.
    – David Conrad
    Jun 26 at 20:55












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.






share|improve this answer















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.







share|improve this answer















share|improve this answer



share|improve this answer








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 with ToUpper/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 on char and should use IsUpper, ToUpper, and ToLower 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











  • 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






  • 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 on char and should use IsUpper, ToUpper, and ToLower 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












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





share|improve this answer



















  • 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















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





share|improve this answer



















  • 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













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





share|improve this answer















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






share|improve this answer















share|improve this answer



share|improve this answer








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 a char, so you save the Line and don't need the new string(char)
    – Corak
    Jun 27 at 8:03













  • 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








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













 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation