regex.replace and trim

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pascal

    regex.replace and trim

    bonjour hello

    I would like to trim a string of all its white spaces so i used
    myString.trim() but it doesn't work as supposed : unsecable space are
    remaining in the middle of my string...
    i read in msdn : and notice that trim only Removes all occurrences of white
    space characters from the beginning and end of this instance. So what for
    the middle ?

    .NET Framework Class Library
    String.Trim Method ()


    Remarks
    The following table lists the white space characters removed by the Trim
    method. The first column lists the Unicode name for the character, and the
    second column lists the Unicode hexadecimal notation for the code point that
    identifies the character.

    Unicode name
    Unicode code point

    CHARACTER TABULATION
    U+0009

    LINE FEED
    U+000A

    LINE TABULATION
    U+000B

    FORM FEED
    U+000C

    CARRIAGE RETURN
    U+000D

    SPACE
    U+0020

    NO-BREAK SPACE
    U+00A0

    EN QUAD
    U+2000

    EM QUAD
    U+2001

    EN SPACE
    U+2002

    EM SPACE
    U+2003

    THREE-PER-EM SPACE
    U+2004

    FOUR-PER-EM SPACE
    U+2005

    SIX-PER-EM SPACE
    U+2006

    FIGURE SPACE
    U+2007

    PUNCTUATION SPACE
    U+2008

    THIN SPACE
    U+2009

    HAIR SPACE
    U+200A

    ZERO WIDTH SPACE
    U+200B

    IDEOGRAPHIC SPACE
    U+3000

    ZERO WIDTH NO-BREAK SPACE
    U+FEFF



    But anything else for the middle than replace ?
    perhaps something with regex.replace ?
    Public Shared Function Replace ( _
    input As String, _
    pattern As String, _
    replacement As String, _
    options As RegexOptions _
    ) As String
    Dim input As String
    Dim pattern() As String 'is it possible to put an array of all white spaces
    here ?Dim replacement As String
    Dim options As RegexOptions
    Dim returnValue As String

    returnValue = Regex.Replace(i nput, pattern, replacement, options)
    End FunctionDoes anyone know a good function for removing all theese kind of
    space in my string ?
    Site de ressources pour l'école élémentaire et plus particulièrement pour le cycle 3. Leçons, cours de français et de maths, exercices interactifs, logiciels gratuits d'apprentissage du calcul mental. De nombreux exerciseurs avec correction en ligne, générateurs d'exercices de maths, orthographe, grammaire et conjugaison.



  • RobinS

    #2
    Re: regex.replace and trim

    Go through the string and check each character.
    The fastest way to do this is to use a
    StringBuilder and append each valid character to it.

    Public Function RemoveWhiteSpac e(ByVal InputString As String) _
    As String
    Dim sb As New StringBuilder(I nputString.Leng th)
    For n As Integer = 0 To InputString.Len gth - 1
    If Not IsWhiteSpace(In putString, n) Then
    sb.Append(Input String.Substrin g(n, 1))
    End If
    Next
    Return sb.ToString
    End Function

    You need to import System.Char for the IsWhiteSpace method,
    and System.Text for the Stringbuilder class.

    Robin S.

    "Pascal" <scalpanospm@wa nadoo.rfwrote in message
    news:454e3184$0 $27387$ba4acef3 @news.orange.fr ...
    bonjour hello
    >
    I would like to trim a string of all its white spaces so i used
    myString.trim() but it doesn't work as supposed : unsecable space are
    remaining in the middle of my string...
    i read in msdn : and notice that trim only Removes all occurrences of
    white space characters from the beginning and end of this instance. So
    what for the middle ?
    >
    .NET Framework Class Library
    String.Trim Method ()
    >
    >
    Remarks
    The following table lists the white space characters removed by the Trim
    method. The first column lists the Unicode name for the character, and the
    second column lists the Unicode hexadecimal notation for the code point
    that identifies the character.
    >
    Unicode name
    Unicode code point
    >
    CHARACTER TABULATION
    U+0009
    >
    LINE FEED
    U+000A
    >
    LINE TABULATION
    U+000B
    >
    FORM FEED
    U+000C
    >
    CARRIAGE RETURN
    U+000D
    >
    SPACE
    U+0020
    >
    NO-BREAK SPACE
    U+00A0
    >
    EN QUAD
    U+2000
    >
    EM QUAD
    U+2001
    >
    EN SPACE
    U+2002
    >
    EM SPACE
    U+2003
    >
    THREE-PER-EM SPACE
    U+2004
    >
    FOUR-PER-EM SPACE
    U+2005
    >
    SIX-PER-EM SPACE
    U+2006
    >
    FIGURE SPACE
    U+2007
    >
    PUNCTUATION SPACE
    U+2008
    >
    THIN SPACE
    U+2009
    >
    HAIR SPACE
    U+200A
    >
    ZERO WIDTH SPACE
    U+200B
    >
    IDEOGRAPHIC SPACE
    U+3000
    >
    ZERO WIDTH NO-BREAK SPACE
    U+FEFF
    >
    >
    >
    But anything else for the middle than replace ?
    perhaps something with regex.replace ?
    Public Shared Function Replace ( _
    input As String, _
    pattern As String, _
    replacement As String, _
    options As RegexOptions _
    ) As String
    Dim input As String
    Dim pattern() As String 'is it possible to put an array of all white
    spaces here ?Dim replacement As String
    Dim options As RegexOptions
    Dim returnValue As String
    >
    returnValue = Regex.Replace(i nput, pattern, replacement, options)
    End FunctionDoes anyone know a good function for removing all theese kind
    of space in my string ?
    Site de ressources pour l'école élémentaire et plus particulièrement pour le cycle 3. Leçons, cours de français et de maths, exercices interactifs, logiciels gratuits d'apprentissage du calcul mental. De nombreux exerciseurs avec correction en ligne, générateurs d'exercices de maths, orthographe, grammaire et conjugaison.

    >

    Comment

    • Pascal

      #3
      Re: regex.replace and trim

      Whoa !!! It works fine........... ..Thanks a lot



      Comment

      • Smokey Grindel

        #4
        Re: regex.replace and trim

        trim does what it says, "trims" stuff off the start of end not the middle..
        if it took from the middle that'd be a substring because it would take from
        that point out

        "Pascal" <scalpanospm@wa nadoo.rfwrote in message
        news:454e3184$0 $27387$ba4acef3 @news.orange.fr ...
        bonjour hello
        >
        I would like to trim a string of all its white spaces so i used
        myString.trim() but it doesn't work as supposed : unsecable space are
        remaining in the middle of my string...
        i read in msdn : and notice that trim only Removes all occurrences of
        white space characters from the beginning and end of this instance. So
        what for the middle ?
        >
        .NET Framework Class Library
        String.Trim Method ()
        >
        >
        Remarks
        The following table lists the white space characters removed by the Trim
        method. The first column lists the Unicode name for the character, and the
        second column lists the Unicode hexadecimal notation for the code point
        that identifies the character.
        >
        Unicode name
        Unicode code point
        >
        CHARACTER TABULATION
        U+0009
        >
        LINE FEED
        U+000A
        >
        LINE TABULATION
        U+000B
        >
        FORM FEED
        U+000C
        >
        CARRIAGE RETURN
        U+000D
        >
        SPACE
        U+0020
        >
        NO-BREAK SPACE
        U+00A0
        >
        EN QUAD
        U+2000
        >
        EM QUAD
        U+2001
        >
        EN SPACE
        U+2002
        >
        EM SPACE
        U+2003
        >
        THREE-PER-EM SPACE
        U+2004
        >
        FOUR-PER-EM SPACE
        U+2005
        >
        SIX-PER-EM SPACE
        U+2006
        >
        FIGURE SPACE
        U+2007
        >
        PUNCTUATION SPACE
        U+2008
        >
        THIN SPACE
        U+2009
        >
        HAIR SPACE
        U+200A
        >
        ZERO WIDTH SPACE
        U+200B
        >
        IDEOGRAPHIC SPACE
        U+3000
        >
        ZERO WIDTH NO-BREAK SPACE
        U+FEFF
        >
        >
        >
        But anything else for the middle than replace ?
        perhaps something with regex.replace ?
        Public Shared Function Replace ( _
        input As String, _
        pattern As String, _
        replacement As String, _
        options As RegexOptions _
        ) As String
        Dim input As String
        Dim pattern() As String 'is it possible to put an array of all white
        spaces here ?Dim replacement As String
        Dim options As RegexOptions
        Dim returnValue As String
        >
        returnValue = Regex.Replace(i nput, pattern, replacement, options)
        End FunctionDoes anyone know a good function for removing all theese kind
        of space in my string ?
        Site de ressources pour l'école élémentaire et plus particulièrement pour le cycle 3. Leçons, cours de français et de maths, exercices interactifs, logiciels gratuits d'apprentissage du calcul mental. De nombreux exerciseurs avec correction en ligne, générateurs d'exercices de maths, orthographe, grammaire et conjugaison.

        >

        Comment

        Working...