检查 C++ 中字符串以什么数字结尾

发布于 2024-07-13 08:29:21 字数 303 浏览 9 评论 0原文

在 C++ MD2 文件加载器中,我有很多框架,每个框架都有一个以数字结尾的名称,例如

  • stand0stand1stand2stand3stand4
  • 如何
  • 内容
  • 包含
  • stand10stand11run0run1run2
  • 字符串
  • ...

获取

不 后面的数字? 例如,将“stand10”更改为“stand”的函数

In a C++ MD2 file loader, I have a lot of frames, each with a name that ends with a number, such as

  • stand0
  • stand1
  • stand2
  • stand3
  • stand4
  • ...
  • stand10
  • stand11
  • run0
  • run1
  • run2

etc.

How do I get what the string is without the number behind? e.g. a function that changed "stand10" to just "stand"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小瓶盖 2024-07-20 08:29:21

字符串::find_last_not_of(“0123456789”)
进而
string::substr()

为您提供最后一个非数字/数字的位置。 只需取前面的所有字符即可,这就是基本名称。

加一以获取字符串末尾的数字序列的开头。

注意:没有错误检查或其他测试。

#include <string>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
   string test = "hellothere4";

   size_t last_char_pos = test.find_last_not_of("0123456789");
   string base = test.substr(0, last_char_pos + 1);

编辑

当您的“基本名称”末尾有数字时,所有解决方案都会出现问题。

例如,如果基本字符串是“base1”,那么您永远无法获得正确的基本名称。 我想你已经意识到了这一点。

或者我错过了什么? 只要基本名称不能在后缀数字之前有数字,它就可以正常工作。

string::find_last_not_of("0123456789")
and then
string::substr()

that gives you the position of the last non digit/number. Just take all the preceding characters and that is the base name.

Increment by one to get the start of the number sequence at the end of the string.

Note: no error checking or other tests.

#include <string>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
   string test = "hellothere4";

   size_t last_char_pos = test.find_last_not_of("0123456789");
   string base = test.substr(0, last_char_pos + 1);

EDIT

there is a problem with ALL the solutions when your "base name" has a number at the end.

for example, if the base string is "base1" then you can never get the proper base name. I assume you already are aware of this.

Or am I missing something? As long as the base name can't have a number at the end just before the postfix number it will work fine.

何时共饮酒 2024-07-20 08:29:21

只是为了展示另一种方式,反向迭代器:

string::reverse_iterator rit = str.rbegin();
while(isdigit(*rit)) ++rit;
std::string new_str(str.begin(), rit.base());

如果你有 boost::bind,你可以让你的生活更轻松

std::string new_str(str.begin(),
    std::find_if(str.rbegin(), str.rend(),
                 !boost::bind(::isdigit, _1)).base());

Just to show another way, reverse iterators:

string::reverse_iterator rit = str.rbegin();
while(isdigit(*rit)) ++rit;
std::string new_str(str.begin(), rit.base());

If you have boost::bind, you can make your life easier

std::string new_str(str.begin(),
    std::find_if(str.rbegin(), str.rend(),
                 !boost::bind(::isdigit, _1)).base());
分開簡單 2024-07-20 08:29:21

为了完成它,用 find_first_of:

string new_string = str.substr(0, str.find_first_of("0123456789"));

just one line :)

另外,对于这些事情,我喜欢使用正则表达式(尽管这种情况很简单):

string new_string = boost::regex_replace(str, boost::regex("[0-9]+$"), "");

Just to complete it, one with find_first_of:

string new_string = str.substr(0, str.find_first_of("0123456789"));

just one line :)

Also, for these things, I like to use regular expressions (althought this case is very simple):

string new_string = boost::regex_replace(str, boost::regex("[0-9]+$"), "");
尴尬癌患者 2024-07-20 08:29:21

C 风格的实现方法:

从左侧开始逐个字符地遍历字符串。 当您读取一个数字时,停止并将其标记为字符串的末尾。

char *curChar = myString;   // Temporary for quicker iteration.

while(*curChar != '\0') {   // Loop through all characters in the string.
    if(isdigit(*curChar)) { // Is the current character a digit?
        *curChar = '\0';    // End the string.
        break;              // No need to loop any more.
    }

    ++curChar;              // Move onto the next character.
}

C-style way of doing it:

Iterate through your string character-by-character, starting from the left. When you read a number, stop, and mark it as the end of your string.

char *curChar = myString;   // Temporary for quicker iteration.

while(*curChar != '\0') {   // Loop through all characters in the string.
    if(isdigit(*curChar)) { // Is the current character a digit?
        *curChar = '\0';    // End the string.
        break;              // No need to loop any more.
    }

    ++curChar;              // Move onto the next character.
}
夏九 2024-07-20 08:29:21

又快又脏,而且不太优雅:

for (int i = str.GetLength()-1; i >= 0; i--)
    {
    if (!isdigit(str.GetAt(i)) break;

    str.SetAt(i,'\0');
    }

Quick and dirty and not too elegant:

for (int i = str.GetLength()-1; i >= 0; i--)
    {
    if (!isdigit(str.GetAt(i)) break;

    str.SetAt(i,'\0');
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文