未分類

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 で、表のキャプション内で改行する方法

\newline を使って次のように書く。 例 \begin{table} \begin{tabular}{ |c|c|c|c| } \hline 1 & 2 & 3 \\ \hlin …

no image

tex の表で、複数行を1行内に入れる方法

multirow パッケージを使っておき、表内で multirow で入力する。 例 \usepackage{multirow} \begin{document} \begin{table} \beg …

no image

tex で frac の意味

tex で frac コマンドは、分数(fraction)を表示するために使う。「バックスラッシュを付けた」 \frac であることに注意。 例 $\frac{2}{3}$ 結果

no image

tex で和集合を入力する方法

\bigcup を使う。 例 $\displaystyle \bigcup _ {i \in \Lambda} A_i $ 結果

no image

matplotlib で x軸・y軸ラベルをつける

plt.xlabel と plt.ylabel を使う。 例 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, …