How to insert/remove hyphen to/from a plain string in c#?

13,966

Solution 1

GetResultsWithOutHyphen is easy (and should return a string instead of void

public string GetResultsWithOutHyphen(string input)
{
    // Removing hyphen after 4 characters logic goes here
    return input.Replace("-", "");
}

for GetResultsWithHyphen, there may be slicker ways to do it, but here's one way:

public string GetResultsWithHyphen(string input)
{

    // append hyphen after 4 characters logic goes here
    string output = "";
    int start = 0;
    while (start < input.Length)
    {
        output += input.Substring(start, Math.Min(4,input.Length - start)) + "-";
        start += 4;
    }
    // remove the trailing dash
    return output.Trim('-');
}

Solution 2

Use regex:

public String GetResultsWithHyphen(String inputString)
{
     return Regex.Replace(inputString, @"(\w{4})(\w{4})(\w{4})(\w{4})(\w{3})",
                                       @"$1-$2-$3-$4-$5");
}

and for removal:

public String GetResultsWithOutHyphen(String inputString)
{
    return inputString.Replace("-", "");
}

Solution 3

Here's the shortest regex I could come up with. It will work on strings of any length. Note that the \B token will prevent it from matching at the end of a string, so you don't have to trim off an extra hyphen as with some answers above.

    using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "6A7FEBFCCC51268FBFF";
            for (int i = 0; i <= text.Length;i++ )
                Console.WriteLine(hyphenate(text.Substring(0, i))); 
        } 

        static string hyphenate(string s)
        {
            var re = new Regex(@"(\w{4}\B)");
            return re.Replace (s, "$1-");
        }

        static string dehyphenate (string s)
        {
            return s.Replace("-", "");
        }
    } 
}

Solution 4

something along the lines of:

public string GetResultsWithHyphen(string inText)
{
    var counter = 0;
    var outString = string.Empty;
    while (counter < inText.Length)
    {
        if (counter % 4 == 0)
            outString = string.Format("{0}-{1}", outString, inText.Substring(counter, 1));
        else
            outString += inText.Substring(counter, 1);
        counter++;
    }
    return outString;
}

This is rough code and may not be perfectly, syntactically correct

Solution 5

public static string GetResultsWithHyphen(string str) {
  return Regex.Replace(str, "(.{4})", "$1-");
  //if you don't want trailing -
  //return Regex.Replace(str, "(.{4})(?!$)", "$1-");
}

public static string GetResultsWithOutHyphen(string str) {            
  //if you just want to remove the hyphens:
  //return input.Replace("-", "");
  //if you REALLY want to remove hyphens only if they occur after 4 places:
   return Regex.Replace(str, "(.{4})-", "$1");
}
Share:
13,966
Idrees Khan
Author by

Idrees Khan

Well, in short. I love working with new technologies in Web, Mobile and Desktop. Techs such Angular, React, .Net, Xamarin, Ionic, Angular, Node are the one i am mostly targeting. I love building new things (of course not buildings) and i am very passionate by playing with many .js frameworks.

Updated on June 14, 2022

Comments

  • Idrees Khan
    Idrees Khan almost 2 years

    I have a string like this;

       string text =  "6A7FEBFCCC51268FBFF";
    

    And I have one method for which I want to insert the logic for appending the hyphen after 4 characters to 'text' variable. So, the output should be like this;

    6A7F-EBFC-CC51-268F-BFF
    

    Appending hyphen to above 'text' variable logic should be inside this method;

    public void GetResultsWithHyphen
    {
         // append hyphen after 4 characters logic goes here
    }
    

    And I want also remove the hyphen from a given string such as 6A7F-EBFC-CC51-268F-BFF. So, removing hyphen from a string logic should be inside this method;

    public void GetResultsWithOutHyphen
    {
         // Removing hyphen after 4 characters logic goes here
    }
    

    How can I do this in C# (for desktop app)? What is the best way to do this? Appreciate everyone's answer in advance.