简体   繁体   English

在最后一次出现字符C ++时删除char数组的开始

[英]Removing start of char array at last occurrence of a character C++

I am looking for the best way to remove the start of a string until the last occurrence of a character. 我正在寻找最好的方法来删除字符串的开始,直到最后一个字符出现。 For example. 例如。 I have a char array that contains the following: 我有一个包含以下内容的char数组:

[Microsoft][ODBC SQL Server Driver][SQL Server]Option 'enable_debug_reports' requires a value of 0 or 1. [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]选项'enable_debug_reports'要求值为0或1。

Basically, I am looking for the last occurrence of ']'. 基本上,我正在寻找最后出现的']'。 I would like my char array to be trimmed to: 我希望将char数组调整为:

Option 'enable_debug_reports' requires a value of 0 or 1. 选项“ enable_debug_reports”要求值为0或1。

I have found several ways to do this with the string data type. 我发现了使用字符串数据类型执行此操作的几种方法。 I am wondering if there is an effective way to manipulate a char array. 我想知道是否有一种有效的方式来操纵char数组。 My program requires several parameters to be char[] instead of strings. 我的程序需要几个参数为char []而不是字符串。 How would I use something like strcpy in my situation? 在这种情况下,我将如何使用诸如strcpy东西?

The below should work provided your string does contain the ']' character: 如果您的字符串确实包含']'字符,则下面的命令应该起作用:

std::string trimIt(originalCStr);
std::string trimmed(trimIt.substr(trimIt.find_last_of("]")));
strcpy(originalCStr, trimmed.c_str());

For a pure C approach: 对于纯C方法:

char *toPtr = originalCStr;
char *fromPtr = strchr(toPtr, ']');
++fromPtr;
while (*fromPtr != '\0') {
    *toPtr = *fromPtr;
    ++fromPtr;
    ++toPtr;
}
*toPtr = '\0';

你可以使用strrchr找到“]”最后一次出现,然后砍你的char []使用的memcpy所看到这里

If you insist on not using std::string , for whatever reason, there is still a pure C++ approach using standard algorithms , which work just fine with raw arrays. 如果出于任何原因坚持不使用std::string ,那么仍然存在使用标准算法的纯C ++方法,该方法对原始数组也能很好地工作。 The following is C++14 (it uses std::rbegin and std::rend ), but you can adapt it to C++11 using std::reverse_iterator manually if necessary: 以下是C ++ 14(它使用std::rbeginstd::rend ),但是您可以根据需要使用std::reverse_iterator手动将其适应C ++ 11:

#include <algorithm>
#include <iostream>

template <class InputRange, class OutputIterator, class Value>
void CopyFrom(InputRange const& input, OutputIterator output_iter, Value const& value)
{
    using std::rbegin;
    using std::rend;
    using std::end;

    auto const iter_last = std::find(rbegin(input), rend(input), value);
    std::copy(iter_last.base(), end(input), output_iter);
}

int main()
{
    char const src[] = "[Microsoft][ODBC SQL Server Driver][SQL Server]Option 'enable_debug_reports' requires a value of 0 or 1.";
    char * dst = new char[sizeof(src) + 1](); // just for this toy program

    CopyFrom(src, dst, ']');

    std::cout << dst;
    delete[] dst;
}

Note that this solution assumes that you need the substring as a copy , and there is no error checking for input that does not contain the specified value. 请注意,此解决方案假定您需要将子字符串作为copy ,并且不对不包含指定值的输入进行错误检查。

And of course, you are probably better of switching to std::string and using c_str() for any C APIs. 当然,您可能最好切换到std::string并将c_str()用于任何C API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM