regex.replace and trim

P

Pascal

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
 
R

RobinS

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.
 
S

Smokey Grindel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top