Mfc-strings
MFC-ストリング
- 文字列*は、文字のシーケンスを表すオブジェクトです。 Cスタイルの文字列は、C言語内で作成され、C ++内で引き続きサポートされます。
- この文字列は、実際にはヌル文字「\ 0」で終了する文字の1次元配列です。
- NULLで終わる文字列には、文字列とそれに続くヌルを構成する文字が含まれます。
文字配列の簡単な例を次に示します。
char word[12] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0' };
以下は、別の表現方法です。
char word[] = "Hello, World";
Microsoft Foundation Class(MFC)ライブラリは、 CString と呼ばれる文字列を操作するクラスを提供します。 以下は、CStringの重要な機能です。
- CStringには基本クラスがありません。
- CStringオブジェクトは、可変長の文字シーケンスで構成されます。
- CStringは、Basicと同様の構文を使用して関数と演算子を提供します。 *連結演算子と比較演算子は、メモリ管理の簡素化とともに、CStringオブジェクトを通常の文字配列よりも使いやすくします。
CStringのコンストラクタは次のとおりです。
Sr.No. | Method & Description |
---|---|
1 |
さまざまな方法でCStringオブジェクトを構築します |
ここに配列メソッドのリストがあります-
Sr.No. | Method & Description |
---|---|
1 |
GetLength CStringオブジェクトの文字数を返します。 |
2 |
IsEmpty CStringオブジェクトに文字が含まれていないかどうかをテストします。 |
3 |
Empty 文字列の長さを0にします。 |
4 |
GetAt 指定された位置の文字を返します。 |
5 |
SetAt 指定された位置に文字を設定します。 |
ここに比較方法のリストがあります-
Sr.No. | Method & Description |
---|---|
1 |
Compare 2つの文字列を比較します(大文字と小文字を区別します)。 |
2 |
CompareNoCase 2つの文字列を比較します(大文字と小文字は区別されません)。 |
ここに抽出方法のリストがあります-
Sr.No. | Method & Description |
---|---|
1 |
Mid 文字列の中央部分を抽出します(Basic MID $関数など)。 |
2 |
Left 文字列の左部分を抽出します(Basic LEFT $関数など)。 |
3 |
Right 文字列の右部分を抽出します(Basic RIGHT $関数など)。 |
4 |
SpanIncluding 指定された文字セットにある文字列から文字を抽出します。 |
5 |
SpanExcluding 指定された文字セットにない文字列から文字を抽出します。 |
変換方法のリストは次のとおりです。
Sr.No. | Method & Description |
---|---|
1 |
MakeUpper この文字列のすべての文字を大文字に変換します。 |
2 |
MakeLower この文字列のすべての文字を小文字に変換します。 |
3 |
MakeReverse この文字列の文字を反転します。 |
4 |
Format sprintfのように文字列をフォーマットします。 |
5 |
TrimLeft 文字列から先頭の空白文字を削除します。 |
6 |
TrimRight 文字列から末尾の空白文字を削除します。 |
以下に検索方法のリストを示します。
Sr.No. | Method & Description |
---|---|
1 |
Find 大きな文字列内の文字または部分文字列を検索します。 |
2 |
ReverseFind 大きな文字列内の文字を検索します。最後から始まります。 |
3 |
FindOneOf セットから最初に一致する文字を検索します。 |
バッファアクセスメソッドのリストを以下に示します。
Sr.No. | Method & Description |
---|---|
1 |
GetBuffer CString内の文字へのポインターを返します。 |
2 |
GetBufferSetLength CString内の文字へのポインターを返し、指定された長さに切り捨てます。 |
3 |
ReleaseBuffer GetBufferによって返されたバッファの制御を解放します |
4 |
FreeExtra 文字列に以前に割り当てられた余分なメモリを解放することにより、この文字列オブジェクトのオーバーヘッドを削除します。 |
5 |
LockBuffer 参照カウントを無効にし、バッファ内の文字列を保護します。 |
6 |
UnlockBuffer 参照カウントを有効にし、バッファ内の文字列を解放します。 |
Windows固有のメソッドのリストを以下に示します。
Sr.No. | Method & Description |
---|---|
1 |
AllocSysString CStringデータからBSTRを割り当てます。 |
2 |
SetSysString CStringオブジェクトからのデータで既存のBSTRオブジェクトを設定します。 |
3 |
LoadString Windows CEリソースから既存のCStringオブジェクトをロードします。 |
CStringオブジェクトのさまざまな操作は次のとおりです-
文字列を作成
文字列リテラルを使用するか、CStringクラスのインスタンスを作成することにより、文字列を作成できます。
BOOL CMFCStringDemoDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
//Set the icon for this dialog. The framework does this automatically
//when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); //Set big icon
SetIcon(m_hIcon, FALSE); //Set small icon
CString string1 = _T("This is a string1");
CString string2("This is a string2");
m_strText.Append(string1 + L"\n");
m_strText.Append(string2);
UpdateData(FALSE);
return TRUE;//return TRUE unless you set the focus to a control
}
上記のコードをコンパイルして実行すると、次の出力が表示されます。
空の文字列
- 空の文字列リテラルを使用するか、CString
- Empty()メソッドを使用して、空の文字列を作成できます。 ブール型プロパティisEmptyを使用して、文字列が空かどうかを確認することもできます。
BOOL CMFCStringDemoDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
//Set the icon for this dialog. The framework does this automatically
//when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); //Set big icon
SetIcon(m_hIcon, FALSE); //Set small icon
CString string1 = _T("");
CString string2;
string2.Empty();
if(string1.IsEmpty())
m_strText.Append(L"String1 is empty\n");
else
m_strText.Append(string1 + L"\n");
if(string2.IsEmpty())
m_strText.Append(L"String2 is empty");
else
m_strText.Append(string2);
UpdateData(FALSE);
return TRUE;//return TRUE unless you set the focus to a control
}
上記のコードをコンパイルして実行すると、次の出力が表示されます。
文字列連結
- 2つ以上の文字列を連結するには、+演算子を使用して2つの文字列またはCString
- Append()メソッドを連結します。
BOOL CMFCStringDemoDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
//Set the icon for this dialog. The framework does this automatically
//when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); //Set big icon
SetIcon(m_hIcon, FALSE); //Set small icon
//To concatenate two CString objects
CString s1 = _T("This "); //Cascading concatenation
s1 += _T("is a ");
CString s2 = _T("test");
CString message = s1;
message.Append(_T("big ") + s2);
//Message contains "This is a big test".
m_strText = L"message: " + message;
UpdateData(FALSE);
return TRUE;//return TRUE unless you set the focus to a control
}
上記のコードをコンパイルして実行すると、次の出力が表示されます。
ストリングの長さ
- 文字列の長さを調べるには、CStringオブジェクトの文字数を返すCString
- GetLength()メソッドを使用できます。
BOOL CMFCStringDemoDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
//Set the icon for this dialog. The framework does this automatically
//when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); //Set big icon
SetIcon(m_hIcon, FALSE); //Set small icon
CString string1 = _T("This is string 1");
int length = string1.GetLength();
CString strLen;
strLen.Format(L"\nString1 contains %d characters", length);
m_strText = string1 + strLen;
UpdateData(FALSE);
return TRUE;//return TRUE unless you set the focus to a control
}
上記のコードをコンパイルして実行すると、次の出力が表示されます。
文字列比較
2つの文字列変数を比較するには、==演算子を使用できます
BOOL CMFCStringDemoDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
//Set the icon for this dialog. The framework does this automatically
//when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); //Set big icon
SetIcon(m_hIcon, FALSE); //Set small icon
CString string1 = _T("Hello");
CString string2 = _T("World");
CString string3 = _T("MFC Tutorial");
CString string4 = _T("MFC Tutorial");
if (string1 == string2)
m_strText = "string1 and string1 are same\n";
else
m_strText = "string1 and string1 are not same\n";
if (string3 == string4)
m_strText += "string3 and string4 are same";
else
m_strText += "string3 and string4 are not same";
UpdateData(FALSE);
return TRUE;//return TRUE unless you set the focus to a control
}
上記のコードをコンパイルして実行すると、次の出力が表示されます。