Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello there,

here my input string :

"amit","30122018","1234567890","please insert correct date"CRLF
"punit","30122018","abcd123457","please insert correct date CRLF
CRLF
**CRLF
please correct mobile no, invalid records"CRLF

//end of input string

Expected string:

"amit","30122018","1234567890","please insert correct date"CRLF
"punit","30122018","abcd123457","please insert correct date

**
please correct mobile no, invalid records"CRLF

//end of expected string

Note : no any third party tools

Thanks,
Azam

What I have tried:

i tried to use with regex but it doesn't work
Posted
Updated 26-Jun-19 3:50am

 
Share this answer
 
You need to look more closely at your data, and work out more accurately exactly what you are trying to do.

At the moment the data you supply is inconsistent: You show some CRLF characters, but your text as required contains "invisible" newlines:
"amit","30122018","1234567890","please insert correct date"CRLF
"punit","30122018","abcd123457","please insert correct date 

**
please correct mobile no, invalid records"CRLF
You show two CRLF pairs, but there are three "hidden" ones inside your final string (because without them your data would look like this:
"amit","30122018","1234567890","please insert correct date"
"punit","30122018","abcd123457","please insert correct date **please correct mobile no, invalid records"
With just two newlines - one at the end of each line.
So start by looking at the data as a byte stream (using a hex editor for example) and work out exactly what you input is. Then decide exactly what you binary output should be. Once you have worked that out, it should be pretty trivial to process: copy up to first double quotes, then find matching quote: remove unwanted newlines between them. Repeat to end of string.

That's a pretty trivial single loop to code!
 
Share this answer
 
Hi, thanks for giving a reply

string s =Regex.Replace(FileData, "\"[^\"]*(?:\"\"[^\"]*)*\"", m => m.Value.Replace("\r", "").Replace("\n",""))

it has been resolved my issues

Thanks,
azam
 
Share this answer
 
v2
 
Share this answer
 
v2

I tend to avoid Regex if at all possible as it's slow and uses convoluted and opaque syntax. If you wish to remove CRLF from quoted text and replace it with a space character, you can do something like the following.

C#
public static string RemoveQoteEmbededCRLF(string text)
{
    bool isQuoted = false;
    StringBuilder sb = new StringBuilder();
    for (int i=0;i<text.Length;i++)
    {
        char c = text[i];
        //look for '"' if found toggle isquoted
        if (c == '"') isQuoted = !isQuoted;
        //if CR found inside quoted text
        if(c=='\r'&& isQuoted&&i <text.Length-1)
        {
            //See if LF is next char
            if (text[i+1]=='\n')
            {
                //CRLF found, replace with space
                c = ' ';
                //step over LF
                i += 1;
            }
        }
        sb.Append(c);
    }
    return sb.ToString();
}

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900