TypeScript 文字列(string)の空白を全削除するサンプル

環境
Windows 11 Pro 64bit
TypeScript 4.4.4

構文
文字列からreplace()を呼び出します。
replace()の第1引数に「/\s+/g」、第2引数に空文字を指定します。
const 変数名: string = text.replace(/\s+/g, ")
呼び出した文字列(string)の空白を削除した文字列を返します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const strA: string = " A  B  C D E F "
const result: string = strA.replace(/\s+/g, '')
console.log(result)
const strA: string = " A  B  C D E F " const result: string = strA.replace(/\s+/g, '') console.log(result)
const strA: string = "   A   B  C D  E  F "
const result: string = strA.replace(/\s+/g, '')
console.log(result)

出力結果:
“ABCDEF"

IT

Posted by arkgame