Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hi,
 
This is wat i have done...But i can see this is not an efficient way...
 
                         For x = 126 To 255
                               If strName.Contains(Chr(x)) Then
                                   strName = strName.Replace(Chr(x), " ")
                                   dr("Name") = strName
 
                               Else
                                   dr("Name") = parts(0)
                               End If
                           Next
 
Can someone suggest me a better way.
 
Thanks.
Posted 14-Mar-13 7:31am
vidkaat533
Comments
Sergey Alexandrovich Kryukov at 14-Mar-13 12:41pm
   
How come these characters are "special". How about some characters you did not take into account, for example: ¡¦§©«®°†‡‖‰‱※‼‽⁂? What, are they not so "special"? :-)
—SA
vidkaat at 14-Mar-13 12:55pm
   
Halo,
 
Chr(126) to chr(255) covers everything .....Here is the link http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/The_Characters.asp.....
Sergey Alexandrovich Kryukov at 14-Mar-13 12:59pm
   
Well, tell this to someone else, not to me. What do you think about the character in your example? Do you have an idea where they come from?
I'll explain. You are talking about ASCII encoding, but .NET supports Unicode, of course. ASCII is a small subset of it. Do you know how Unicode works.
 
Anyway, I provided you a comprehensive answer on performance problem. Please accept it formally (green button)...
—SA
Sarrrva at 14-Mar-13 13:01pm
   
hi try
Public Function IsSpecialChar(ByVal c As Char) As Boolean
' Need you to fill this out
End Function
 
Public Function RemoveSpecialChars(ByVal s As String) As String
Dim builder = New System.Text.StringBuilder()
Dim cur
For Each cur In s
If Not IsSpecialChar(cur) Then
builder.Append(cur)
End If
Next
Return builder.ToString()
End Function
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
You can use a regular expression to replace special characters with whitespace. In this example, all characters which aren't a-z, A-Z or 0-9 will be replaced with whitespace:
Dim input As String = "Hello^world" ' change this into your input
Dim replaced As String = System.Text.RegularExpressions.Regex.Replace(input, "[^a-zA-Z0-9]", " ")
Console.WriteLine(replaced) ' output: Hello world
More about regular expressions:
The 30 Minute Regex Tutorial[^]
Regex.Replace(String, String, String) method:
http://msdn.microsoft.com/en-us/library/e7f5w83z.aspx[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The first improvement you could make is to get rid of that completely.
If the string does not contain the char value 255, then dr("name") will always contain parts(0) and (presumably) the revised strName value will be discarded - because the final value in the loop determines the value of the array element.
So I assume that is not what you are trying to do...
 
What are you trying to achieve?
The reason I ask is that unicode characters (which is what strings are made up of) are not necessarily just in the range 0..255 If you are after printable characters only then it's relatively simple - but I would probably use a Regex
 

"I dont want to declare the special characters. I shud use ch(x) where x=126 to 255 ...I want to basically check for the special characters in a string. If found replace them with a space else i want to store the string in my datarow dr."
 
What about the special characters below space? Don't you want to get rid of them too?
Can I suggest that you start with a list of *permitted* characters and use a regex to replace all the characters which aren't on it? It's a simpler solution:
Public Dim regex As Regex = New Regex("[^\w\.\,!""$%^&*\(\)-_+=::@']")
 

' This is the replacement string
Public Dim regexReplace As String = " "
 

' Replace the matched text in the InputText using the replacement pattern
Dim result As String = regex.Replace(InputText,regexReplace)
  Permalink  
v2
Comments
vidkaat at 14-Mar-13 12:50pm
   
I dont want to declare the special characters. I shud use ch(x) where x=126 to 255 ...I want to basically check for the special characters in a string. If found replace them with a space else i want to store the string in my datarow dr.
OriginalGriff at 14-Mar-13 13:00pm
   
Answer updated
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There is lot of better way,
 
trivial code snippet,
 
import your namespace,
Imports System.Text.RegularExpressions
 
write your own function,
 
Public Shared Function RemoveSpecialCharacters(ByVal input As String) As String
       Dim r As New Regex("(?:[^a-z0-9 ]|(?<=['""])s)", RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant Or RegexOptions.Compiled)
       Return r.Replace(input, [String].Empty)
   End Function
 
usage:
 MsgBox("Result :" & RemoveSpecialCharacters("C# language"))
 
regards
sarva
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

First of all, the purpose itself is quite questionable. Please see my comment to the question.
 
Indeed, your code is utterly ineffective. You don't see to understand that the type string is immutable. The method string.Replace does not actually modify any string as this is impossible. Instead, it creates a brand new string each time you call it, and initialize this new string with a modified copy of original string. You assignment simply abandons old reference to string data, and it is eventually garbage-collected. Enormously wasteful. And you make it even worse by iterating by character range (weird concept of "special") and repeating search every time. You would have to through out search and iterate by string's characters instead. But don't do it. You should never use even Replace in a cycle.
 
Instead, you should use mutable type System.Text.StringBuilder:
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx[^].
 
You need to start with empty instance of System.Text.StringBuilder and append some characters just ignoring some others. Note that System.Text.StringBuilder.ToStrings will give you the resulting string.
 
Through out all your code, try again.
 
—SA
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 CPallini 155
3 PIEBALDconsult 150
4 BillWoodruff 148
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 14 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100