Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 2nd, 2010, 03:08 AM
Authorized User
 
Join Date: Dec 2007
Location: , , Vietnam.
Posts: 59
Thanks: 9
Thanked 0 Times in 0 Posts
Post How can I remove hyphens from string?

Hello everybody


I need to remove empty spaces and hyphens from strings (or any non alphanumeric characters).Can you please help and point me to somewhere i can find help?

ex: nha----trang --live
turn into
nha-trang-live



Thanks,


Phuc Hoang
  #2 (permalink)  
Old February 2nd, 2010, 04:27 AM
Friend of Wrox
Points: 2,473, Level: 20
Points: 2,473, Level: 20 Points: 2,473, Level: 20 Points: 2,473, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: India
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

You can create function something like:
Code:
public string RemoveSpecialChars(string str) 
    { 
 //you can add more chars as well !!
 string[] chars = new string[] { ",", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'" }; 
 for (int i = 0; i < chars.Length; i++) 
 { 
     if (str.Contains(chars[i])) 
     { 
  str = str.Replace(chars[i], ""); 
     } 
 } 
 return str; 
  }
This can be called as:
Code:
  string originalS = "T!t^t%!s#f&'#;$%m|i%t(te)  [o:h]i)t@*t}est";
  string newString = RemoveSpecialChars(originalS);
or use regular experssion to do the same.
Code:
  string s = Regex.Replace(originalS, "[^\\w\\.@-]", "");
__________________
Om Prakash Pant
Click the "Thanks" button if this post helped you.
The Following User Says Thank You to om_prakash For This Useful Post:
phuc2583 (February 2nd, 2010)
  #3 (permalink)  
Old February 2nd, 2010, 05:55 AM
Authorized User
 
Join Date: Dec 2007
Location: , , Vietnam.
Posts: 59
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Thanks for replying me to do that
and i have convert your code into vb.net like this:

Public Shared Function RemoveSpecialChars(ByVal str As String) As String
'you can add more chars as well !!
Dim chars() As String = New String() {",", ".", "/", "!", "", "#", "$", "%", "^", "&", "*", "'"}
Dim i As Integer
For i = 0 To chars.Length - 1 Step i + 1
If str.Contains(chars(i)) Then
str = str.Replace(chars(i), "")
End If
Next
Return str
End Function


i use this function and i have met an error as " String cannot be of zero length.
Parameter name: oldValue


"
Thanks all
  #4 (permalink)  
Old February 3rd, 2010, 12:52 AM
Friend of Wrox
Points: 2,473, Level: 20
Points: 2,473, Level: 20 Points: 2,473, Level: 20 Points: 2,473, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: India
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

Please check the following code:
Code:
 
Dim originalS AsString = "T,.E/s! #T^"
Dim newString AsString = RemoveSpecialChars(originalS)
 
PublicSharedFunction RemoveSpecialChars(ByVal str AsString) AsString
'you can add more chars as well !!
Dim chars() AsString = NewString() {",", ".", "/", "!", " ", "#", "$", "%", "^", "&", "*", "'"}
Dim i AsInteger
For i = 0 To chars.Length - 1 Step i + 1
If str.Contains(chars(i)) Then
str = str.Replace(chars(i), "")
EndIf
Next
Return str
EndFunction
__________________
Om Prakash Pant
Click the "Thanks" button if this post helped you.
The Following User Says Thank You to om_prakash For This Useful Post:
phuc2583 (February 4th, 2010)
  #5 (permalink)  
Old February 16th, 2010, 04:02 AM
Registered User
 
Join Date: Feb 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

'String Manipulation - How to Remove two or more hyphens from a string
Sub main()
Dim sText As String
sText = "This test-string. This is -- the test-string. " & _
"This is --------- the test-string. This is - the ------ " & _
"test - string. This is the test--string."
Debug.Print sText
Do While InStr(sText, "--") > 0
sText = Replace(sText, "---", "--")
sText = Replace(sText, "--", "")
sText = Replace(sText, " ", " ")
Loop
Debug.Print sText
End Sub

'Or

Sub main()
'You Need to set reference to Microsoft VBScript Regular Expressions 5.5
Dim sText As String
Dim re As RegExp, m As MatchCollection
Set re = New RegExp

sText = "This test-string. This is -- the test-string. " & _
"This is --------- the test-string. This is - the ------ " & _
"test - string. This is the test--string."
Debug.Print sText

re.Global = True
re.Pattern = "[-]{2,}"
sText = re.Replace(sText, "")
re.Pattern = "[ ]{2,}"
sText = re.Replace(sText, " ")
Debug.Print sText
End Sub

May this will help you...........
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove non-number parts of string fixit XSLT 4 October 15th, 2009 12:59 PM
remove commas and space in a string kumiko Classic ASP Basics 1 January 10th, 2008 11:42 PM
Remove the unwanted space at end of the string gmbalaa General .NET 2 August 21st, 2007 11:35 PM
Stripping hyphens arholly Access 2 January 4th, 2007 09:16 AM
Replace or remove the last char from the string sabu21s XSLT 12 January 8th, 2004 05:26 PM



All times are GMT -4. The time now is 05:52 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.