如何从在C字符串中删除引号 [英] How to remove quotes from a string in C

查看:198
本文介绍了如何从在C字符串中删除引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除某一行中的所有报价,除了一个反斜杠后跟报价

I am trying to remove all quotes in a given line except a backslash followed by a quote

我所做的就是这个。

for (int i = 0; i < lineLength; i ++) {

        if (line[i] == '"' ) { 
                if (line[i-1] == '\\') // if \" is used   
                        line[i-1] = '"'; // then print \
                line[i] = '\0'; // or 0
        }
}

这将删除该行的全部字符..我能做些什么来只删除引号?
任何帮助将是AP preciated ...

This removes all characters in the line.. what can I do to remove only quotes? Any help would be appreciated...

推荐答案

您的问题是行[I] ='\\ 0'; - 它终止字符串

Your problem is line[i] = '\0'; - it terminates the string.

如果您想从C字符串中删除字符,你需要容纳两个指数 - 一个用于读,一个用于写,环比读索引读取每个字符,只写那些你想用第二个保持指数。

If you want to remove characters from a C string, you need to hold two indices - one for reading and one for writing, loop over the read index reading each character, and write only the ones you want to keep using the second index.

线沿线的东西:

int j = 0;
for (int i = 0; i < lineLength; i ++) {
            if (line[i] != '"' && line[i] != '\\') { 
                 line[j++] = line[i];
            } else if (line[i+1] == '"' && line[i] == '\\') { 
                 line[j++] = '"';
            } else if (line[i+1] != '"' && line[i] == '\\') { 
                 line[j++] = '\\';
            }
}

//You missed the string termination ;)
if(j>0) line[j]=0;

这篇关于如何从在C字符串中删除引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆