未分類

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 の積分範囲の分数サイズを変更する方法

定積分の分数の大きさを調整するには、\mbox のサイズ指定を \small や \normalsize や \large 等に設定すると良い。 例 $\displaystyle \int _0 ^{ …

no image

python3 で、ファイルの文字列の1行目を出力する

ファイルを読み込んで、1行目だけをコンソールに出力するには readlines() を使う。 例 f = open(“data.txt”) lines = f.readlines() print(li …

no image

python3 で数字をゼロ埋めして表示する

string の、zfill を使う。 例 n = 3 n0 = str(n).zfill(10) print(n0) 結果 0000000003

no image

tex で期待値記号を入力する方法

\mathbf を使う。 例 $\mathbf{E}(\xi)$ 結果

no image

C言語で文字をアルファベット順にずらす

C言語で文字をずらすには、char 型の変数に1を加えればよい。 例 #include <stdio.h> int main(void) { char c; c = ‘a’; c++; p …