How to insert / remove hyphen to / from a simple string in C #?

I have a line like this:

   string text =  "6A7FEBFCCC51268FBFF";

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

6A7F-EBFC-CC51-268F-BFF

The transfer of variables to the text variable above should be added to this method;

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

And I want to also remove the hyphen from the given string , for example 6A7F-EBFC-CC51-268F-BFF. Thus, removing this hyphen from string logic should be inside this method;

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

How to do it in C # (for desktop application)? What is the best way to do this? Appreciate each answer in advance.

+5
source share
9 answers

GetResultsWithOutHyphen ( string void

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

GetResultsWithHyphen, , :

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('-');
}
+7

:

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

:

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

. . , \B , , .

    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("-", "");
        }
    } 
}
+2
var hyphenText = new string(
  text
 .SelectMany((i, ch) => i%4 == 3 && i != text.Length-1 ? new[]{ch, '-'} : new[]{ch})
 .ToArray()

)

+1
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");
}
+1

- :

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;
}

,

+1

:

String textHyphenRemoved=text.Replace('-',''); should remove all of the hyphens

StringBuilder strBuilder = new StringBuilder();
int startPos = 0;
for (int i = 0; i < text.Length / 4; i++)
{
    startPos = i * 4;
    strBuilder.Append(text.Substring(startPos,4));

    //if it isn't the end of the string add a hyphen
    if(text.Length-startPos!=4)
        strBuilder.Append("-");
}
//add what is left
strBuilder.Append(text.Substring(startPos, 4));
string textWithHyphens = strBuilder.ToString();

, .

0

GetResultsWithOutHyphen method

public string GetResultsWithOutHyphen(string input)
{

     return input.Replace("-", "");

}

GetResultsWithOutHyphen method

.

public string GetResultsWithHyphen(string input)
{

string output = "";
        int start = 0;
        while (start < input.Length)
        {
            char bla = input[start];
            output += bla;
            start += 1;
            if (start % 4 == 0)
            {
                output += "-";    
            }
        }
return output;
}
0

This worked for me when I had a value for the social security number (123456789) and he needed to display it as (123-45-6789) in the list.

ListBox1.Items.Add("SS Number :  " & vbTab & Format(SSNArray(i), "###-##-####"))

In this case, I had an array of social security numbers. This line of code changes the formatting to insert a hyphen.

0
source

All Articles