How can I trim spaces in the string?

admin

Administrator
Staff member
Hi All:

I got a problem when I am trying to trim a string. e.g. The string is "This is a business firm." How can I trim the spaces in this string. Also, I would like to know during the trimming, can I trim all the characters excepts the 26 alphates? Thnaks for the attention!

Best rgds,
Ivan VongYou could do a replace statement to get rid of spaces
replace("string", "this", "with this")I think "StringTokenizer" is what you are looking for:

<!-- m --><a class="postlink" href="http://www.codeproject.com/csharp/StringTokenizer.aspI">http://www.codeproject.com/csharp/StringTokenizer.aspI</a><!-- m --> think "StringTokenizer" is what you are looking for:

<!-- m --><a class="postlink" href="http://www.codeproject.com/csharp/StringTokenizer.asp">http://www.codeproject.com/csharp/StringTokenizer.asp</a><!-- m --> If you used that you would then have to concatenate many small strings after the string is broken down. You would also have to populate the fragments into an array.can I trim all the characters excepts the 26 alphates



You could do a replace statement to get rid of spaces


in order to use the replace function to trim characters which are beyond the 26 alphates, would ivong need to create another array just to hold those special characters and keep looping through the array?

What are some other effective approach he can choose when wanted to avoid using StringTokenizer?Well replace would just get rid of the spaces.

What might be done is loop through the string and move each letter into a new string. This would not be efficient if you are dealing with exceedingly long strings though. You could conceivably do this using one if statement within a for loop... but that if statement would involve a lot of ors.Dim x As String = "asdf 3434 asd /.,/2asfas"
Dim y As String
For Each C As Char In x.ToCharArray
If Char.IsLetter(C) Then y &= C
Next
 
Back
Top