65.9K
CodeProject is changing. Read more.
Home

How to Toggle String Case in .NET

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Feb 14, 2011

CPOL
viewsIcon

7168

What about this?string s = new string(( from c in "ABCdef" select char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c) ).ToArray());Note by Alexander Bell: In regards to the performance, this one (i.e. Alternate 9) is the worst in comparison...

What about this?
string s = new string((
            from c in "ABCdef" 
            select char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c) 
           ).ToArray());
Note by Alexander Bell: In regards to the performance, this one (i.e. Alternate 9) is the worst in comparison with Alternate 1 and Alternate 2, also it requires Linq Library, which negatively impacts the Algorithm portability: Alternate 9 is NOT RECOMMENDED