| 方法 | 用途 | 地區化 | 回傳值 |
|---|---|---|---|
toUpperCase() | 轉大寫 | 否 (通用 Unicode) | 新字串 |
toLowerCase() | 轉小寫 | 否 (通用 Unicode) | 新字串 |
toLocaleUpperCase([locale]) | 轉大寫 (地區化) | 是 (依語系規則) | 新字串 |
toLocaleLowerCase([locale]) | 轉小寫 (地區化) | 是 (依語系規則) | 新字串 |
const str = "Hello World";
console.log(str.toUpperCase()); // "HELLO WORLD"
console.log(str.toLowerCase()); // "hello world"
console.log(str); // "Hello World" (原字串未變!)
// 串連使用
const normalized = str.toLowerCase().trim(); // "hello world"
// 實務:不區分大小寫比較
function equalsIgnoreCase(a, b) {
return a.toLowerCase() === b.toLowerCase();
}
console.log(equalsIgnoreCase("Greg LeMond", "greg lemond")); // true
console.log(equalsIgnoreCase("GREG LEMOND", "Greg Lemond")); // true
Unicode 定義了通用大小寫映射,但部分語言有特殊規則:
const str = "DİYARBAKIR"; // 土耳其語城市名
console.log(str.toLowerCase()); // "diyarbakır" (點號 i - 通用規則)
console.log(str.toLocaleLowerCase("tr")); // "diyarbakır" (相同)
const str2 = "DIYARBAKIR"; // 大寫 I 無點
console.log(str2.toLowerCase()); // "diyarbakır" (點號 i)
console.log(str2.toLocaleLowerCase("tr")); // "diyarbakır" (無點 ı) ← 關鍵差異!
// 反向:小寫轉大寫
console.log("ı".toUpperCase()); // "I" (通用)
console.log("ı".toLocaleUpperCase("tr")); // "I" (相同)
console.log("i".toUpperCase()); // "I" (通用)
console.log("i".toLocaleUpperCase("tr")); // "İ" (帶點大寫 I) ← 關鍵差異!
| 語言 | 代碼 | 特殊規則 |
|---|---|---|
| 土耳其語 | tr, az (亞塞拜然) | i ↔ İ, ı ↔ I (四種對應) |
| 德語 | de | ß → SS (大寫),ES6 新增 "ß".toUpperCase() === "SS" |
| 希臘語 | el | Σ (sigma) 詞尾變 ς,大小寫映射特殊 |
| 立陶宛語 | lt | 特定字母組合大小寫規則 |
| 荷蘭語 | nl | IJ/ij 作為單一字母單位 |
// 登入:Email 不區分大小寫,但密碼區分
function normalizeEmail(email) {
return email.toLowerCase().trim(); // RFC 5321: local-part 大小寫敏感,但實務多視為不敏感
}
// 搜尋:統一轉小寫比對
function search(users, query) {
const q = query.toLowerCase();
return users.filter(u =>
u.name.toLowerCase().includes(q) ||
u.email.toLowerCase().includes(q)
);
}
// 將使用者輸入轉為有效 CSS 類名
function toClassName(str) {
return str.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^a-z0-9-_]/g, "");
}
console.log(toClassName("My Component")); // "my-component"
console.log(toClassName("用戶頭像")); // "用戶頭像" (中文保留,視專案決定是否轉拼音)
const items = ["Apple", "apple", "Banana", "başlangıç"];
// 通用排序 (Unicode 碼點順序)
console.log([...items].sort());
// ["Apple", "Banana", "apple", "başlangıç"] (大寫在前)
// 地區化排序 (Intl.Collator - 推薦)
const collator = new Intl.Collator("zh-TW", { sensitivity: "base" });
console.log([...items].sort(collator.compare));
// 正確處理語言規則,如土耳其語 ı/i 排序
| 場景 | 建議 | 理由 |
|---|---|---|
| 大量字串轉換 | 避免在迴圈中重複呼叫 | 建立轉換後的查找表/Map |
| 不區分大小寫比較 | toLowerCase() 優於 toLocaleLowerCase() | 無地區化開銷、結果一致 |
| 使用者可見文字顯示 | 使用 toLocaleUpperCase( navigator.language ) | 尊重用戶語系設定 |
| 內部 ID、Key、Code 處理 | 嚴格使用 toLowerCase() | 確保跨環境一致性 |
let str = "hello";
str.toUpperCase();
console.log(str); // "hello" - 未改變!
let val = null;
val.toLowerCase(); // TypeError: Cannot read property 'toLowerCase' of null
const safeLower = (val) => val == null ? "" : String(val).toLowerCase();
String.prototype.isWellFormed() / toWellFormed()處理單獨代理項 (lone surrogates) 的 Unicode 字串,避免轉換時產生無效字符。現代瀏覽器已開始支援。
JavaScript 提供四種大小寫轉換方法,核心差異在於是否套用地區化規則。99% 場景使用通用版 toLowerCase()/toUpperCase() 即可,僅在處理使用者可見文字、特定語系排序、土耳其語/德語等特殊語言時才需 toLocaleLowerCase()。永遠記住:字串不可變,方法回傳新字串,務必接收回傳值。