0
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?

JSで文字列操作をしよう

Posted at

参考講座

文字列を受け取って処理しよう

//ユーザーから文字列を受け取り操作するプログラム
const string = prompt('message');
console.log(string);

//文字列の長さを表示
console.log(string.length);

//文字列の特定の1文字にアクセス → 0から始まるindex
console.log(string[0]);

2025-09-27 18.06の画像.jpeg

文字列を整形しよう

//名前当てクイズの作成
  const string = prompt('Name?');
  //全て小文字にする
  if (string.toLowerCase() === 'rio') {
  //全て大文字にする
  if (string.toUpperCase() === 'RIO') {
    console.log('Correct!')
  } else {
    console.log('Wrong!');
  }
//前後の空白を消す
if (string.toUpperCase().trim() === 'RIO') {

※ユーザーから入力を受け取るときは、何が入力されるか分からないので、上記のような命令で整形してから、比較する手法がよく使われる

複数の文字列を処理しよう

const emails = [
    'rio@example.com',
    'rioto@example.com',
    'ryu@example.com',
  ]

  emails.forEach((email) => {
    //特定の文字列が含まれるかどうかを'includes'判別
    if (email.includes('rio') === true) {
      console.log(email);
    }
  });

2025-09-27 18.32の画像.jpeg

//'rio'から始まる文字だけ抜き出したい
emails.forEach((email) => {
  
  //①'rio'が最初に来るindexを教えてくれる
  if (email.indexOf('rio') === 0){
    console.log(email);
  }

  //②特定の文字で始まるかどうかを判定
  if (email.startsWith('rio') === true) {
    console.log(email);
  }
  
});

2025-09-27 18.41の画像.jpeg

slice()で部分文字列を切り出そう

const emails = [
  'rio@example.com',
  'o-rio@example.com',
  'ryu@example.com',
]

emails.forEach((email) => {
  //文字列の一部を切り出す
  console.log(email.slice(0, 4));
});

2025-09-27 18.48の画像.jpeg

emails.forEach((email) => {
  //@前の文字列を切り出す
  const loc = email.indexOf('@');
  console.log(email.slice(0, loc));
});

2025-09-27 18.51の画像.jpeg

console.log(email.substring(0, loc));

slice()と同じ出力結果を出す。全く同じではないがslice()の方が高機能!

replace(),split()で文字列を処理しよう


  const emails = [
    'rio@example.com',
    'o-rio@example.com',
    'ryu@example.com',
  ]

  emails.forEach((email) => {
    //最初に見つけた文字列の置換をする
    console.log(email.replace('@example.com', ''));
    //複数箇所全てを置換する
    console.log(email.replaceAll('@example.com', ''));
  });
  emails.forEach((email) => {
  //@を区切りにした配列にする
  const items = email.split('@');
  console.log(items[0]);
  });

2025-09-27 18.51の画像.jpeg

repeat()で文字列を繰り返してみよう

  //最近見た映画の本数の管理
  const counts = [2, 13, 8, 17];

  //本数分*で表示する
  counts.forEach((count) => {
    let bar = '';
    for (let i = 0; i < count; i++) {
      bar += '*';
    }
    console.log(bar);

↓ 短く書く方法

  counts.forEach((count) => {
    const bar = '*'.repeat(count);
    console.log(bar);
  });

2025-09-27 21.50の画像.jpeg

padStart(),padEnd()で文字列を整形しよう

  //最近見た映画の本数の管理
  const counts = [2, 13, 8, 17];

  counts.forEach((count) => {
    const bar = '*'.repeat(count);
    //テンプレート文字列で表を分かりやすくする
    console.log(`${count}:${bar}`);
  });

2025-09-27 21.58の画像.jpeg

  //最近見た映画の本数の管理
  const counts = [2, 13, 8, 17];

  counts.forEach((count) => {
    const bar = '*'.repeat(count);

    //文字列が特定の桁に満たない場合、何かしらの文字で埋める

    //前を埋める
    const label = String(count).padStart(2, ' ');

    //後ろを埋める
    const label = String(count).padEnd(2, ' ');

    console.log(`${label}:${bar}`);
  });


2025-09-27 22.09の画像.jpeg

後ろ
2025-09-27 22.12の画像.jpeg

0
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
0
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?