未分類

c++ で文字列を「区切り文字」を使って分ける方法

投稿日:

string の find を使って、区切り文字の場所を取得し、その位置で区切る。

#include <iostream>
#include <string>

using namespace std;
int main ()
{
string str1 = "Good.morning";

string kugiri_moji = "."; // 区切り文字
string sub1 = str1.substr(0, str1.find(kugiri_moji));
string sub2 = str1.substr(str1.find(kugiri_moji)+1, str1.size());

cout << "part1 is " << sub1 << endl;
cout << "part2 is " << sub2 << endl;

return 0;
}

結果

part1 is Good
part2 is morning

-未分類

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

tex で数式モード内に普通の文字列を入力する方法

\textrm を使う。 例 $$y = f (x) \hspace{1cm} \textrm{This is a function.}$$ 結果

no image

tex で文字列を変数として設定する方法

\newcommand を使って指定する。入力の数は自由に指定できる。 例 \newcommand{\variable}[2]{Hello #1. Good #2.} \variable{Taro}{ …

no image

python で文字列の長さを表示する方法

len を使う。 例 length = len(‘文字列あいうえお’) print(str(length)) 結果 8

no image

matplotlib で縦横の罫線を出力する

罫線を出力するには、pyplot.grid を使う。 例 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 …

no image

python3 でカンマの入った数値の文字列を数値に変換する方法

replace と int 変換を組み合わせる。 例 str1 = ‘1,234,567’ int1 = int(str1.replace(‘,’,”)) print(int1) 結果 123456 …