C++ 替换C+中字符串中的特定字符+;
我有一个代码如下-C++ 替换C+中字符串中的特定字符+;,c++,C++,我有一个代码如下- Value = "Current &HT"; //this is value void StringSet(const char * Value) { const char *Chk = NULL; Chk = strpbrk(Value,"&"); if(Chk != NULL) { strncpy(const_cast<char *> (Chk),"&",4) } } Value=“
Value = "Current &HT"; //this is value
void StringSet(const char * Value)
{
const char *Chk = NULL;
Chk = strpbrk(Value,"&");
if(Chk != NULL)
{
strncpy(const_cast<char *> (Chk),"&",4)
}
}
Value=“当前&HT”//这就是价值
无效字符串集(常量字符*值)
{
const char*Chk=NULL;
Chk=strpbrk(值“&”);
如果(Chk!=NULL)
{
strncpy(施工铸件(Chk),“和”,4)
}
}
在上面的代码中,我想用“&”替换“&”from Value。如果我有“&”单个字符,那么效果很好,但在当前情况下,strpbrk()返回“&HT”,在下面的strncpy中,替换整个“&HT”
现在我想知道只能替换字符串中单个字符的方法。您不应该修改常量字符串,当然也不能修改字符串文字。虽然使用
std::string
比自己处理资源管理要好得多,但一种方法是分配一个新的c风格的st响铃并返回指向它的指针:
char *StringSet(const char *Value) {
char buffer[256];
for (char *p = (char*)Value, *t = buffer; p[0] != 0; p++, t++) {
t[0] = p[0];
if (p[0] == '&') {
t[1] = 'a'; t[2] = 'm'; t[3] = 'p';
t += 3;
}
t[1] = 0;
}
char *t = new char[strlen(buffer)+1];
strcpy(t, buffer);
return t;
}
您不应该修改常量字符串,当然也不能修改字符串文字。虽然使用
std::string
比自己处理资源管理要好得多,但一种方法是分配一个新的c样式字符串并返回指向它的指针:
char *StringSet(const char *Value) {
char buffer[256];
for (char *p = (char*)Value, *t = buffer; p[0] != 0; p++, t++) {
t[0] = p[0];
if (p[0] == '&') {
t[1] = 'a'; t[2] = 'm'; t[3] = 'p';
t += 3;
}
t[1] = 0;
}
char *t = new char[strlen(buffer)+1];
strcpy(t, buffer);
return t;
}
您不能用几个字符替换C样式字符串中的一个字符,因为您无法知道C样式字符串中有多少空间可以添加新字符。您只能通过分配一个新字符串并将旧字符串复制到新字符串来完成此操作。类似于
char* StringSet(const char* value)
{
// calculate how many bytes we need
size_t bytes = strlen(value) + 1;
for (const char* p = value; *p; ++p)
if (*p == '&')
bytes += 3;
// allocate the new string
char* new_value = new char[bytes];
// copy the old to the new and replace any & with &
char* q = new_value;
for (const char* p = value; *p; ++p)
{
*q = *p;
++q;
if (*p == '&')
{
memcpy(q, "amp", 3);
q += 3;
}
}
*q = '\0';
return new_value;
}
但这是一段糟糕的代码。你真的应该使用std::string。你不能用几个字符替换C样式字符串中的一个字符,因为你无法知道在C样式字符串中有多少空间可以添加新字符。你只能通过分配一个新字符串并将旧字符串复制到新字符串来实现。类似这样的事情
char* StringSet(const char* value)
{
// calculate how many bytes we need
size_t bytes = strlen(value) + 1;
for (const char* p = value; *p; ++p)
if (*p == '&')
bytes += 3;
// allocate the new string
char* new_value = new char[bytes];
// copy the old to the new and replace any & with &
char* q = new_value;
for (const char* p = value; *p; ++p)
{
*q = *p;
++q;
if (*p == '&')
{
memcpy(q, "amp", 3);
q += 3;
}
}
*q = '\0';
return new_value;
}
但这是一段糟糕的代码。你真的应该使用std::string。我认为你需要一些临时数组来保存字符串,然后替换原始字符串中的&并将临时数组附加到原始字符串中。这是上面修改的代码,我相信你可以使用strstrstr而不是strchr。它接受char*作为第二个参数
void StringSet(char * Value)
{
char *Chk = NULL,*ptr = NULL;
Chk = strchr(Value,'&');
if(Chk != NULL)
{
ptr = Chk + 1;
char* p = (char*)malloc(sizeof(char) * strlen(ptr));
strcpy(p,ptr);
Value[Chk-Value] = '\0';
strcat(Value,"&");
strcat(Value,p);
free(p);
}
}
谢谢
Niraj Rathi我认为您需要一些临时数组来保存字符串,然后替换原始字符串中的字符串,并将临时数组附加到原始字符串中。这是上面修改的代码,我相信您可以使用strstrstr而不是strchr。它接受char*作为第二个参数
void StringSet(char * Value)
{
char *Chk = NULL,*ptr = NULL;
Chk = strchr(Value,'&');
if(Chk != NULL)
{
ptr = Chk + 1;
char* p = (char*)malloc(sizeof(char) * strlen(ptr));
strcpy(p,ptr);
Value[Chk-Value] = '\0';
strcat(Value,"&");
strcat(Value,p);
free(p);
}
}
谢谢
Niraj Rathi为什么不使用
std::string
呢?它有一个replace
函数。我想使用这个函数,但由于我在一个已经定义的程序上工作,我受到一些限制,不能使用std::string。为什么不使用std::string
呢?它有一个replace
函数。我想使用它,但是当我在一个已经定义的程序上工作时,我受到一些限制,不能使用std::string。“我在一个已经定义的程序上工作,不能使用std::string”“我在一个已经定义的程序上工作,不能使用std::string。”“如果你正在做所有的工作来计算最终的字符串大小和迭代复制,你可以很容易地将值
固定到位(使用反向迭代),这似乎是海报代码的意图。当然,这有可能导致缓冲区溢出+“你真的应该使用std::string”。@TonyD嗯,我说你不知道你有多少可用空间,这就是我的观点。另外还有一个问题,就是用字符串文本调用这个函数。非常好的一点,根据这一点,您选择的折衷方案看起来很棒。干杯。如果你正在做所有的工作来计算最终的字符串大小和迭代复制,你可以很容易地将值
固定到位(使用反向迭代),这似乎是海报代码的意图。当然,这有可能导致缓冲区溢出+“你真的应该使用std::string”。@TonyD嗯,我说你不知道你有多少可用空间,这就是我的观点。另外还有一个问题,就是用字符串文本调用这个函数。非常好的一点,根据这一点,您选择的折衷方案看起来很棒。干杯