未分類

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 で email を書く方法

\hyperref パッケージを使って次のように書く。 例 \usepackage{hyperref} \begin{document} \href{mailto:taro@example.co.jp …

no image

tex で文字を上付きにした矢印を入力する方法

\overset を使う方法がある。 例 $\overset{\text{abc}}{\rightarrow}$ 結果

no image

python3 で文字列を区切り文字を指定してリストに変換する方法

split メソッドを使う。 例 str1 = ‘this, is, a, pen’ str2 = str1.split(‘,’) print(str2) 結果 [‘this’, ‘ is’, ‘ a …

no image

tex で組み合わせ記号(nCk)を入力する

tex で組み合わせ記号を入力するには、アンダーバー(_)を使う。C はローマン体にする。 例 ${}_n {\mathrm C}_k$ 結果

no image

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

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