Click here to Skip to main content
15,905,914 members
Home / Discussions / C#
   

C#

 
GeneralRe: Reduce multiple characters to one, in a string! Pin
Adeel Chaudhry6-May-07 4:17
Adeel Chaudhry6-May-07 4:17 
GeneralRe: Reduce multiple characters to one, in a string! Pin
Rob Graham6-May-07 4:40
Rob Graham6-May-07 4:40 
GeneralRe: Reduce multiple characters to one, in a string! Pin
Luc Pattyn6-May-07 5:25
sitebuilderLuc Pattyn6-May-07 5:25 
GeneralRe: Reduce multiple characters to one, in a string! Pin
Adeel Chaudhry6-May-07 4:15
Adeel Chaudhry6-May-07 4:15 
AnswerRe: Reduce multiple characters to one, in a string! Pin
Guffa6-May-07 4:25
Guffa6-May-07 4:25 
GeneralRe: Reduce multiple characters to one, in a string! Pin
Adeel Chaudhry6-May-07 4:36
Adeel Chaudhry6-May-07 4:36 
GeneralRe: Reduce multiple characters to one, in a string! Pin
PIEBALDconsult6-May-07 6:52
mvePIEBALDconsult6-May-07 6:52 
AnswerRe: Reduce multiple characters to one, in a string! Pin
PIEBALDconsult6-May-07 4:41
mvePIEBALDconsult6-May-07 4:41 
I happen to have one here, one I wrote in C many years ago and then ported to C# a few years back, thanks for giving me a reason to revisit it. (Now I'll revisit my Split that honors quotes and escapes as well.)

Usage:
subject = the string to compress
trash = list of characters to compress (usually " \b" -- SPACE and TAB)
replacements = what to replace them with (usually " " -- SPACE)
quotes = if you don't want compress within quotes, specify "\"" or "'"
escapes = not often used, but if for instance trash contains "n" and subject contains "\n"
and you don't want it replaced, specify "\\" for escapes and the "\n" will be
preserved

(quotes and escapes may be empty or null)

advanced: two characters may be specified for replacements (and I've forgotten why I allow it)
given trash="z" and replacements="xy"
then subject="zzz" becomes "yyx" (rather than "x")

example:
C#
if ( args.Length > 0 )
{
    System.Console.Write 
    ( 
        "<" + 
        PIEBALD.Lib.LibStr.Compress ( args [ 0 ] , " \b" , " " , "'" , "\\" ) 
        + ">" 
    ) ;
}


C#
/**************************************************************************************************************/
/*  Compress sequences of "trash" characters down to one                                                      */

        public static string
        Compress
        (
            string subject      ,
            string trash        ,
            string replacements ,
            string quotes       ,
            string escapes
        )
        {
            System.Text.StringBuilder result = new System.Text.StringBuilder ( subject.Length ) ;

            char prevch = '\0' ;
            int  index  = 0    ;
            int  quo    = -1   ;
            int  quoon  = -1   ;
            int  escon  = -1   ;

            if ( quotes == null )
            {
                quotes = "" ;
            }

            if ( escapes == null )
            {
                escapes = "" ;
            }

            while ( index < subject.Length )
            {
                if ( ( escon = escapes.IndexOf ( prevch ) ) != -1 )
                {
                    quoon = -1 ;
                }
                else
                {
                    quoon = quotes.IndexOf ( subject [ index ] ) ;
                }

                if ( quoon != -1 )
                {
                    if ( quo == -1 )
                    {
                        quo = quoon ;
                    }
                    else
                    {
                        if ( quo == quoon )
                        {
                            quo = -1 ;
                        }
                    }
                }

                prevch = subject [ index ] ;

                if ( ( quo == -1 ) && ( trash.IndexOf ( subject [ index ] ) != -1 ) )
                {
                    if ( ( index == subject.Length-1 ) || ( trash.IndexOf ( subject [ index+1 ] ) == -1 ) )
                    {
                        result.Append ( replacements [ 0 ] ) ;
                    }
                    else
                    {
                        if ( replacements.Length > 1 )
                        {
                            result.Append ( replacements [ 1 ] ) ;
                        }
                    }

                    index++ ;
                }
                else
                {
                    result.Append ( subject [ index++ ] ) ;
                }
            }

            return ( result.ToString() ) ;
        }

GeneralRe: Reduce multiple characters to one, in a string! Pin
Adeel Chaudhry6-May-07 4:54
Adeel Chaudhry6-May-07 4:54 
GeneralRe: Reduce multiple characters to one, in a string! Pin
PIEBALDconsult6-May-07 4:57
mvePIEBALDconsult6-May-07 4:57 
AnswerRe: Reduce multiple characters to one, in a string! Pin
AFSEKI7-May-07 2:09
AFSEKI7-May-07 2:09 
QuestionTreeview challenge Pin
Rick van Woudenberg6-May-07 3:07
Rick van Woudenberg6-May-07 3:07 
AnswerRe: Treeview challenge Pin
Arun.Immanuel6-May-07 3:54
Arun.Immanuel6-May-07 3:54 
GeneralRe: Treeview challenge Pin
Rick van Woudenberg6-May-07 4:23
Rick van Woudenberg6-May-07 4:23 
JokeRe: Treeview challenge Pin
PIEBALDconsult6-May-07 5:03
mvePIEBALDconsult6-May-07 5:03 
GeneralRe: Treeview challenge Pin
Rick van Woudenberg6-May-07 9:31
Rick van Woudenberg6-May-07 9:31 
GeneralRe: Treeview challenge Pin
PIEBALDconsult6-May-07 18:18
mvePIEBALDconsult6-May-07 18:18 
QuestionPlugIns - Preventing Malicious Code... Pin
Shy Agam6-May-07 2:49
Shy Agam6-May-07 2:49 
AnswerRe: PlugIns - Preventing Malicious Code... Pin
PIEBALDconsult6-May-07 18:20
mvePIEBALDconsult6-May-07 18:20 
AnswerRe: PlugIns - Preventing Malicious Code... Pin
AFSEKI7-May-07 2:19
AFSEKI7-May-07 2:19 
Questiondot or comma? Pin
Ranger496-May-07 0:26
Ranger496-May-07 0:26 
AnswerRe: dot or comma? Pin
Ranger496-May-07 1:35
Ranger496-May-07 1:35 
AnswerRe: dot or comma? [modified] Pin
DavidNohejl6-May-07 1:59
DavidNohejl6-May-07 1:59 
GeneralRe: dot or comma? Pin
Ranger496-May-07 2:23
Ranger496-May-07 2:23 
AnswerRe: dot or comma? Pin
AFSEKI7-May-07 2:21
AFSEKI7-May-07 2:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.