JavaScript sliceメソッドで後ろから指定した数の文字列を削除する

環境
Google Chrome 111.0.5563.147
Windows 10 Home 64bit
構文
文字列.slice(0,マイナス値);
sliceメソッドを使うと、後ろから指定した数の文字列を削除します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let str = "テストデータ";
// 後ろから1文字を削除する
let resA = str.slice(0,-1);
console.log(resA);
// 後ろから3文字を削除する
let resB = str.slice(0,-3);
console.log(resB);
let str = "テストデータ"; // 後ろから1文字を削除する let resA = str.slice(0,-1); console.log(resA); // 後ろから3文字を削除する let resB = str.slice(0,-3); console.log(resB);
let str = "テストデータ";

// 後ろから1文字を削除する
let resA = str.slice(0,-1);
console.log(resA); 

// 後ろから3文字を削除する
let resB = str.slice(0,-3);
console.log(resB);

結果
> “テストデー"
> “テスト"

JavaScript

Posted by arkgame