Can you reverse order a string in one line with LINQ or a LAMBDA expression

前端 未结 11 874
暗喜
暗喜 2021-02-02 16:01

Not that I would want to use this practically (for many reasons) but out of strict curiousity I would like to know if there is a way to reverse order a string using LINQ and/or

相关标签:
11条回答
  • 2021-02-02 16:26

    In addition to one previous post here is a more performant solution.

    var actual0 = "reverse me".Aggregate(new StringBuilder(), (x, y) => x.Insert(0, y)).ToString();
    
    0 讨论(0)
  • 2021-02-02 16:27
    var reversedValue= "reverse me".Reverse().ToArray();
    
    0 讨论(0)
  • 2021-02-02 16:33

    I don't see a practical use for this but just for the sake of fun:

    new string(Enumerable.Range(1, input.Length).Select(i => input[input.Length - i]).ToArray())
    
    0 讨论(0)
  • 2021-02-02 16:34

    If we need to support combining characters and surrogate pairs:

    // This method tries to handle:
    // (1) Combining characters
    // These are two or more Unicode characters that are combined into one glyph.
    // For example, try reversing "Not nai\u0308ve.". The diaresis (¨) should stay over the i, not move to the v.
    // (2) Surrogate pairs
    // These are Unicode characters whose code points exceed U+FFFF (so are not in "plane 0").
    // To be represented with 16-bit 'char' values (which are really UTF-16 code units), one character needs *two* char values, a so-called surrogate pair.
    // For example, try "The sphere \U0001D54A and the torus \U0001D54B.". The                                                                     
    0 讨论(0)
  • 2021-02-02 16:35
    new string(value.Reverse().ToArray())
    
    0 讨论(0)
提交回复
热议问题