1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

文字列の一部を追加したりする操作って、意外と最初はピンとこない…。今回は文字列の挿入問題について解説する!



🔍 問題概要

文字列 S, T と整数 N が与えられるので、SN文字目の「後ろ」に T を挿入して出力せよ。

入力例:

abcde
fghij
5

出力例:

abcdefghij



🛠 解き方:sliceで分割&合体

const rl = require('readline').createInterface({ input: process.stdin });
const lines = [];

rl.on('line', line => lines.push(line));
rl.on('close', () => {

    const S = lines[0];
    const T = lines[1];
    const N = Number(lines[2]);


    // S の N文字目の後ろ → index N で分割(0-indexed)
    const result = S.slice(0, N) + T + S.slice(N);

    console.log(result);
});
  • N は 1-based(1 文字目が 1)
  • slice は 0-based(1 文字目が index 0)
    この一行で「前半 + T + 後半」が完成! slice は本当に便利。




💡他のコード例

「他のやり方」や「新しいメソッド」に挑戦するのも良い練習になるので、以下にいくつかの別解を紹介する!

方法①:配列に変換して splice() を使う

const rl = require('readline').createInterface({ input: process.stdin });
const lines = [];

rl.on('line', line => lines.push(line));
rl.on('close', () => {
    const S = lines[0];
    const T = lines[1];
    const N = Number(lines[2]);



    const arr = S.split('');     // 文字列を配列に分解
    arr.splice(N, 0, T);         // N 番目の位置に T を挿入
    console.log(arr.join(''));   // 配列を文字列に戻す
});

方法②:substring() を使う

const result = S.substring(0, N) + T + S.substring(N);

slice() と似てるが、引数が負のときの挙動が違う。


方法③:replace() + 正規表現(ちょっと強引)

const result = S.replace(new RegExp(`^(.{${N}})`), `$1${T}`);
  • ^(.{N}) は先頭から N 文字をキャプチャ
  • $1${T} でその直後に T を挿入

方法④:スプレッド構文を使って splice()

const arr = [...S]; // スプレッド構文で配列化
arr.splice(N, 0, T);
console.log(arr.join(''));



✅ 気づきメモ

  • slice()で切ってつなぐのが一番直感的でシンプル!
  • splice()は配列操作だから、ちょっと大げさ
  • substring()でも同じことはできるけど、負の数の扱いがちょっと違うので注意
  • replace()+正規表現でもゴリ押しできるけど、読みづらい


新しく学んだこと

  • slice()substring() の違い:負の数の扱いに注意
  • splice() は配列にしか使えない

まとめ

ちょっとした操作でも、いろんなやり方があるのが面白いところ!
これからも、「他のやり方」や「新しいメソッド」に挑戦したい!




僕の失敗談(´;ω;`)と解決法🐈

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?