未分類

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 でオーム記号を入力する方法

siunit のパッケージを使って、\si{\ohm} で入力する。 例 \usepackage{siunitx} \begin{document} \si{\ohm} \end{document} …

no image

mac の convert コマンドで画像ファイルをサイズ変更する

convert コマンドを使うには、imagemagick というソフトがインストールされていることが必要。 インストールの確認 $ which convert で、imagemagick がインスト …

no image

tex の分数のシグマ記号(和)がつぶれないようにする

分子・分母にシグマ記号(和記号)を使うと、小さくつぶれて見にくいことがある。対策として、\displaystyle を使う。 例 $\frac{\displaystyle \sum_{i=1}^{\i …

no image

python3 で、文字列の末尾の文字を削除する方法

[:-1] で、最後の文字を取り除いた文字列を取得することができる。 例 str1 = “こんにちは。” result = str1[:-1] print(result) 結果 こんにちは

no image

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

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