By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,990 Members | 2,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,990 IT Pros & Developers. It's quick & easy.

regex.replace and trim

P: n/a
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(input, pattern, replacement, options)
End FunctionDoes anyone know a good function for removing all theese kind of
space in my string ?
http://www.scalpa.info
Nov 5 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 RemoveWhiteSpace(ByVal InputString As String) _
As String
Dim sb As New StringBuilder(InputString.Length)
For n As Integer = 0 To InputString.Length - 1
If Not IsWhiteSpace(InputString, n) Then
sb.Append(InputString.Substring(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" <sc*********@wanadoo.rfwrote in message
news:45***********************@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(input, pattern, replacement, options)
End FunctionDoes anyone know a good function for removing all theese kind
of space in my string ?
http://www.scalpa.info

Nov 5 '06 #2

P: n/a
Whoa !!! It works fine.............Thanks a lot

Nov 5 '06 #3

P: n/a
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" <sc*********@wanadoo.rfwrote in message
news:45***********************@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(input, pattern, replacement, options)
End FunctionDoes anyone know a good function for removing all theese kind
of space in my string ?
http://www.scalpa.info

Nov 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.