未分類

c++ で文字列を合成(連結)する方法

投稿日:

c言語では、sprintf 関数を使って文字列を合成(連結)することができる。

いっぽう、c++ では、文字列を string で表して「+」演算子で文字列を合成することができる。

#include <iostream>
#include <string>

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

string gousei = str1 + str2;

cout << gousei << endl;

return 0;
}

結果

Good morning

-未分類

執筆者:


comment

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

関連記事

no image

python3 で、文字列が別の文字列に含まれているかどうか判定する方法

in 演算子を使うことが最も簡単。 例 str1 = “こんにちは” str2 = “んにち” if str2 in str1:  print(“含まれています”) else:  print(“含まれ …

no image

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

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

no image

tex で小文字のL(リットル)記号を入力する方法

tex で小文字のL(l)リットル記号を入力するには、\ell を使う。通常の小文字の l は、\\l であるのに注意。 例 5 \\ell 結果

no image

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

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

no image

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

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