Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3431834
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:22:31+00:00 2026-05-18T07:22:31+00:00

My project has the need to build consistent urls similar to the ones here

  • 0

My project has the need to build consistent urls similar to the ones here on stackoverflow. I know how I “can” do it by running the string through multiple filters, but I’m wondering if I can do it all with a single method.

Basically I want to remove all special characters, and replace them with dashes BUT if there are multiple dashes in a row, I need them to be a single dash. How can I implement this as clean as possible?

Example: If I were to use the following string.

My #1 Event

My regex would create the following string

my–1-event

notice how there are two dashes (one for the space and one for the “#” symbol). What I need is

my-1-event

Here’s how I’m implementing it currently

    ''# <System.Runtime.CompilerServices.Extension()>
    Public Function ToUrlFriendlyString(ByVal input As String) As String
        Dim reg As New Regex("[^A-Za-z0-9]")
        ''# I could run a loop filter here to match "--" and replace it with "-"
        ''# but that seems like more overhead than necessary.
        Return (reg.Replace(Trim(input), "-"))
    End Function

And then all I do is call the extension method

    Dim UrlFriendlyString = MyTile.ToUrlFriendlyString

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:22:31+00:00Added an answer on May 18, 2026 at 7:22 am

    Add a + to the end of the regex.
    This will tell it to match one or more characters that match the character class that precedes the +.

    Also, you should create your Regex instance in a Shared field outside the method so that .Net won’t need to parse the regex again every time you call the method.

    [edited by rockinthesixstring]: here’s the final result

        Private UrlRegex As Regex = New Regex("[^a-z0-9]+", RegexOptions.IgnoreCase)
    
        <System.Runtime.CompilerServices.Extension()>
        Public Function ToUrlFriendlyString(ByVal input As String) As String
            Return (UrlRegex.Replace(Trim(input), "-"))
        End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My company has got a new project upcoming and i need software that can
I need to know, what renderer has been used in the Sintel project. I
I need build in web query capability similar to what you can do in
My project has multiple themes with different colors. I need to skin certain textboxes
in my project i need a tow tables each of it has about 2000
I need to analyse a large Xcode project for its cyclomatic complexity . Has
I got an eclipse project which has more than 10 packages. I need to
I am setting up a build environment for few projects. Each project has more
Env.: VS2008 C# project I need to build my app for use in 2
I'm working in a C/C++ mixed project that has the following situation. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.