c++ - 在 GetDlgItem() 之后修剪 CString

标签 c++ mfc trim c-strings

希望有人能帮我解决这个问题!

我有一个对话框,其中包含一些填充有数据的组合框,用户应该填写这些数据,然后单击保存。当您点击保存时,程序会创建一个包含所选数据的输出文件。

我的问题是我需要在保存文件之前删除连字符处的所有内容!

组合框由看起来像这样的字符串填充:

  • 4010-第一
  • 4020 秒

我希望它在修剪后看起来像这样:

  • 4010
  • 4020

和:

  • PH-彼得·汉森
  • JK-约翰·金

我希望它在修剪后看起来像这样:

  • PH
  • JK

我使用 Visual Studio 6.0 和 MFC。

这是 OnOK 代码:

void CExportChoices::OnOK() 
{

CString sFileName, name, height, weight, age, haircolor, eyecolor, initials, group;


CWnd* pWnd = GetDlgItem(IDC_NAME);
pWnd->GetWindowText(name);

sFileName.Format(".\\Export\\%s_export%d.txt", name, GetTickCount());
ofstream outfile(sFileName,ios::out);


pWnd = GetDlgItem(IDC_HEIGHT);
pWnd->GetWindowText(height);

pWnd = GetDlgItem(IDC_WEIGHT);
pWnd->GetWindowText(weight);

pWnd = GetDlgItem(IDC_AGE);
pWnd->GetWindowText(age);

pWnd = GetDlgItem(IDC_HAIRCOLOR);
pWnd->GetWindowText(haircolor);

pWnd = GetDlgItem(IDC_EYECOLOR);
pWnd->GetWindowText(eyecolor);

pWnd = GetDlgItem(IDC_INITIALS);
pWnd->GetWindowText(initials);

pWnd = GetDlgItem(IDC_GROUP);
pWnd->GetWindowText(group);


outfile << "Height="        <<      height      <<      "\n";
outfile << "\n";
outfile << "Weight="        <<      weight      <<      "\n";
outfile << "\n";
outfile << "Age="           <<      age         <<      "\n";
outfile << "\n"; 
outfile << "Hair color="    <<      haircolor   <<      "\n";
outfile << "\n";
outfile << "Eye color="     <<      eyecolor    <<      "\n";
outfile << "\n";
outfile << "Initials="      <<      initials    <<      "\n";
outfile << "\n";
outfile << "Group="         <<      group       <<      "\n";

outfile.close();

CDialog::EndDialog(22);

}

提前致谢!

------------------------------------更新------ ------------------------------

好吧,经过一番困惑后,我终于找到了一个对我有用的解决方案..

在你们给我的建议之后,这是我尝试做的事情:

来自 ComboBox 的数据:

“4010组”

我的代码:

pWnd = GetDlgItem(IDC_GROUP);
pWnd->GetWindowText(group);

int i = group.Find("-");

if (i >= 0)
{
group = group.Mid(0, i);

}

MessageBox(group); // results = 4010-group

没用。

我认为可能存在一些与 UNICODE 相关的问题,所以我将 ComboBox 中的数据从 "4010-group" 更改为 "4010 group" 并尝试了这个:

pWnd = GetDlgItem(IDC_GROUP);
pWnd->GetWindowText(group);

int i = group.Find(" ");

if (i >= 0)
{
group = group.Mid(0, i);

}

MessageBox(group); // results = 4010

有效!但我不明白为什么连字符不起作用,有人知道吗?

最佳答案

你可以使用CString::FindCString::Mid,这类似于wstring::findwstring::substr

另见 CString functions

CString s = L"4010-First";

int i = s.Find('-');
if (i >= 0)
{
    s = s.Mid(0, i);
    TRACE(L"%s\n", s); //output: "4010"
}

或获取第一部分和第二部分:

CString s1 = s.Mid(0, i);
CString s2 = s.Mid(i + 1);
TRACE(L"(%s)(%s)\n", s1, s2); //output: (4010)(First)

关于c++ - 在 GetDlgItem() 之后修剪 CString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33341306/

相关文章:

c++ - 为什么使用 std::bind 出现参数错误

jQuery 'get' 没有从 C++ FastCGI 应用程序(nginx 网络服务器)收到正确的答案

c++ - 使用 MFC 拦截来自 child 的 child 的消息

c++ - Windows Search C++(MFC) CFindFiles 路径分隔符

visual-studio-2010 - 如何向 MFC 功能区按钮添加图标

JavaScript 用正则表达式 trim 换行符?

java - 如何在修剪后的字符串上添加 "..."?

jquery - 将输入字段值修剪为仅字母数字字符/使用 .使用 jQuery

c++ - 编写可以处理隐式共享但对不可复制类型(如 unique_ptr)关闭它的容器?

c++ - 从 C/C++ 播放声音的最简单方法