Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to replace multiple character in the string with one character.
Example I have string
C#
8 0495: <0x221> [7] 02 20 4e 65 04 a5 a5

output would be like
C#
8 0495,0x221,7,02 20 4e 65 04 a5 a5


rather than replacing one by one

What I have tried:

I am replacing each character separately with ,
Posted
Updated 19-Feb-16 23:16pm
Comments
Andreas Gieriet 19-Feb-16 8:14am    
Some part is missing in the "What I have tried". With what code?
Andi
[no name] 19-Feb-16 13:18pm    
You can use regular expressions(regex) to do this one

Some suggestions: string replace c - Google Search[^].
 
Share this answer
 
Comments
XamBEE 19-Feb-16 7:51am    
i am already replacing characters separately. But i am asking to replace all at once??
Andreas Gieriet 19-Feb-16 8:13am    
What you mean by "all at once"? In the end, each character has to be inspected individually. If this is by your own loop or within some read-to-use function is irrelevant. Or am I missing something?
Cheers
Andi
Richard MacCutchan 19-Feb-16 8:28am    
Then follow some of those links and you will find out how to do it. Or you could always try writing your own method to do it.
This is probably the most efficient.

C++
char data[] = "8 0495: <0x221> [7] 02 20 4e 65 04 a5 a5";

static void replace(char *data)
{
    const char *cursor = data;
    while (*cursor)
    {
        // replace first pattern
        if ( memcmp(cursor, ": <", 3) == 0)
        {
            cursor += 3;
            *data++ = ',';
        }
        // replace second pattern
        else if ( memcmp(cursor, "> [", 3) == 0)
        {
            cursor += 3;
            *data++ = ',';
        }
        // replace third pattern
        else if ( memcmp(cursor, "] ", 2) == 0)
        {
            cursor += 2;
            *data++ = ',';
        }
        else
        {
            cursor++;
            data++;
        } 
    }
    *data++ = 0;
}


If you prefer to work with STL strings, try this:

string::replace - C++ Reference[^]
 
Share this answer
 
You can use another dynamic array, and save the index number of all of the characters in the string and replace all of them once by ' , '.
 
Share this answer
 
Comments
Andreas Gieriet 20-Feb-16 14:00pm    
Seriously? Not really, right? I mean, preparing some data structure to then replace unrelated use of some characters is not leading anywhere. And: how to remove (i.e. replace by nothing)?
Regards
Andi
Suryakant Turing 21-Feb-16 4:49am    
So, either u can use a function, and add a condition in it, i.e. whenever a character(that u want to replace) matches the condition then replace it with " , "
Andreas Gieriet 21-Feb-16 5:07am    
I don't get it.
I understand the OP that he want a "more efficient" solution. Building a (hash) table of all characters and their positions to only tweak with a certain character on all their instances is not very efficient nor robust. E.g. a comma might have a meaning within some string, but another outside the string. So, this approach is rather useless in my eyes...
Regards
Andi

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