---
# System prepended metadata

title: 709. To Lower Case
tags: [LeetCode]

---

---
tags: LeetCode
---

# 709. To Lower Case

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

- Example 1:
Input: "Hello"
Output: "hello"
- Example 2:
Input: "here"
Output: "here"
- Example 3:
Input: "LOVELY"
Output: "lovely"


輸入範本如下
```C#
public class Solution {
    public string ToLowerCase(string str) {
        
    }
}
```

---
### 直覺想法
- 怎麼判斷這個字是否大小寫.
- Char 字元轉成 ASCII 後如何運算
- ASCII 碼如何再轉回 Char 字元
- C# 真是懶人用的語言 = = , 我印象中 JAVA 好像不能直接做字元的運算以及比較 , 需要先轉型成 int 型態.(?)

```C#
public string ToLowerCase(string str)
{
     StringBuilder stringBuilder = new StringBuilder();
     foreach (var c in str)
     {
         // (c + ('a' - 'A')) 是 int32 型態
          stringBuilder.Append(c <= 'Z' && c >= 'A' ? (char)(c + ('a' - 'A')) : c);
     }
     return stringBuilder.ToString();
}
```

#### Linq 寫法
```C#
public string ToLowerCase(string str) => str.Select(c => c <= 'Z' && c >= 'A' ? (char)(c + ('a' - 'A')) : c)
                                        .Aggregate(new StringBuilder(), (builder, r) => builder.Append(r))
                                        .ToString();
```








### Thank you! 

You can find me on

- [GitHub](https://github.com/s0920832252)
- [Facebook](https://www.facebook.com/fourtune.chen)

若有謬誤 , 煩請告知 , 新手發帖請多包涵

# :100: :muscle: :tada: :sheep: 
