How can I replace a part of a string by itself plus the character "~"?

9

How can I replace a part of a string with itself plus the "~" character?

I am doing this as follows: only when the string has two equal numbers like the 51 that comes shortly after AP and the contained in 17 51 3322 Replace does the exchange in both places and I just want to make Replace in the whole number.

My string should look like this:

  

RUASANTA HELENA, 769 ~ AP 51 ~ BL H JD ALVORADA ~ 17513322 ~

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string lista = "RUASANTA HELENA, 769  AP 51 BL H JD ALVORADA~ 17513322 ";

        var match = Regex.Match(lista, "[0-9]+");
        while (match.Success)
        {
            lista = lista.Replace(match.Value, match.Value + "~");
            match = match.NextMatch();
        }
        System.Console.Write(lista);    
    }       
}
    
asked by anonymous 15.06.2016 / 20:49

3 answers

17

If the complete number is always followed by a space, you can use the pattern " ([0-9]+?) " and replace with the match that occurred within the parentheses plus ~ and a space gave match too):

string lista = "RUASANTA HELENA, 769  AP 51 BL H JD ALVORADA~ 17513322 ";
lista = Regex.Replace(lista, "([0-9]) ", "$1~ ");

Example in DotNetFiddle.

    
15.06.2016 / 22:01
6

The question quoted RegEx and the accepted answer gave a good solution. I'd rather do it manually because I make it easier than I would with RegEx, but I know that's not the case at all. If performance is important RegEx is not always a good option. Almost all algorithms can be done faster if done manually. This is what I did:

public static string MudaEndereco(string texto, char adicao = '~') {
    var resultado = new StringBuilder(texto.Length * 2);
    var anterior = '
Regex.Replace(lista, "(\d+) ", "$1~ ")
'; foreach (var caractere in texto) { if (Char.IsDigit(anterior) && Char.IsWhiteSpace(caractere)) { resultado.Append(adicao); } resultado.Append(caractere); anterior = caractere; } return resultado.ToString(); }

See working on dotNetFiddle and on CodingGround (gave to test with more iterations).

RegEx lost to the manual algorithm on average for at least 4X. There have been dozens cases that I disregarded, perhaps because of garbage collection. 4X is no small thing. Depending on the machine the difference was 6X or more. I do not understand RegEx so much, I may have done something wrong, but I did upon what was answered. I tried some optimizations that .Net RegEx allows and only got worse:).

I used up a criterion that may have slowed down because I did not just pick up a white character, I picked up any character that is considered white, if the requirement does not allow it, just change to a simple ' ' . Digit checking is also done in a way that works where the numbers are represented in a non-common way in the Unicode table, it certainly also slows down.

I made an extra example more customizable and the result was pretty much the same.

Note that I gave an optimized RegEx pattern, ? made no sense there and could use \d . So I would:

public static string MudaEndereco(string texto, char adicao = '~') {
    var resultado = new StringBuilder(texto.Length * 2);
    var anterior = '
Regex.Replace(lista, "(\d+) ", "$1~ ")
'; foreach (var caractere in texto) { if (Char.IsDigit(anterior) && Char.IsWhiteSpace(caractere)) { resultado.Append(adicao); } resultado.Append(caractere); anterior = caractere; } return resultado.ToString(); }

So if the answer has to be RegEx this would be my code.

    
05.11.2016 / 01:04
4

String.Replace Method (String, String)

  

Returns a new string in which all occurrences of a String   specified by another specified String.

Syntax

public string Replace(
    string oldValue,
    string newValue
)

Example

public class Example
{
   public static void Main()
   {
      String s = "aaa";
      Console.WriteLine("The initial string: '{0}'", s);
      s = s.Replace("a", a+"~");
      Console.WriteLine("The final string: '{0}'", s);
   }
}

String.Concat Method (String, String)

  

Concatenates two specified String instances.

Syntax

public static string Concat(
    string str0,
    string str1
)

Example

string str0 = "teste";
string str1 = "~";

str0 = string.Concat(str0, str1)

Good luck!

    
28.08.2016 / 03:30