Changing a random string of characters using Stringbuilder

Asked

Viewed 313 times

4

I have the following variables::

String mascara = "12****3*59**100*";
String numero = "12345768"

There is a way to replace the asterisks of my variable mask by the values of my variable number?

Example applied in variable masquerade:

12 1234 3 5 59 76 100 8

I am using . net framework 3.5

4 answers

3


Since you explicitly mentioned one StringBuilder, here’s an example:

        var mascara = "12****3*59**100*";
        var numero = "12345768".ToCharArray();
        var mascarr = mascara.Split('*');
        var buffer = new StringBuilder();

        for (var index = 0; index < mascarr.Length - 1; index++)
        {
            buffer.Append(mascarr[index] + numero[index]);
        }

        var result = buffer.ToString();
        // Resultado: 1212343559761008

For each asterisk present in the mask, the third line of code will generate an item in the array mascarr. Thus:

inserir a descrição da imagem aqui

The next block is just a loop between all members of the array, adding the number relative to the loop cursor and adding everything to its object StringBuilder.

(EDIT: As well mentioned by @Luizvieira in the comments, it is interesting to mention that the last member of the array should be ignored - because it was not itself delimited by an asterisk. So the loop just goes up mascarr.Length - 1. The original code already had this stipulated value, but this post did not explain the reason.)

  • Great solution. But it seems to me that the array actually has 1 item more than the number of asterisks (because "*" was used as the delimiter in the separation). It’s just a matter of being precise in the information. : ) P.S.: Incidentally, improvement suggestion: this fact could be used to validate the data before processing, because if by chance the number of characters in numero is less than the number of asterisks in mascara, your loop will generate an error while trying to index numero[index]).

  • @Luizvieira you are correct about the size of the array - so the loop for goes up to mascarr.Length - 1 instead of mascarr.Length: The last member of the array should be ignored because it was not itself delimited by an asterisk. As for validation, I also agree - but I thought it would be preferable to have an excerpt of code that only dealt with the main points of the OP issue. Anyway, I appreciate the comments!

  • Yes, I didn’t mean that the code is wrong (by the way, your solution is quite clever in my opinion). My comment is more in the sense that the mention in the text practically says that Split "generates an item for each asterisk, "and that’s not exactly true. My concern is only that this mention may induce a wrong understanding of the behavior of the method for those who are learning. Sorry for the confusion - I mentioned finding the number of items to be n+1 only to amend the validation suggestion. :)

  • 1

    @Luizvieira No stress, next time I pay the chopp. =)

1

You will not need a Builder string to replace one character with another... you can use an array:

var rnd = new Random();
var str1 = "12****3*59**100*";
var str2 = "12345768";
var chars = str1.ToCharArray();
for (int it = 0; it < chars.Length; it++)
{
    if (chars[it] == '*')
        chars[it] = str2[rnd.Next(0, str2.Length)];
}
  1. First, I converted the string containing the asterisks into an array using ToCharArray.

  2. Then I run the array by replacing the characters '*' one by one.

  3. The substitution character comes from another string, at a random position, using the class Random.

1

A very concise and readable way:

int numeroIndex = 0;
var array = mascara.Select(
    ch => ch == '*' ? numero[numeroIndex++] : ch)
                  .ToArray();

var result = new string(array);

For each Cluster ch in string mascara, if Character is '*', Substitumos ch by the next character in the string numero.

0

(I don’t know C#, but I suppose this solution (in C++) works, with the right changes).

int posicao = 0; // qual dos numeros na sua string numero estamos
for (unsigned i = 0; i < mascara.size(); ++i) {
    if (mascara[i] == '*') {
        // Se temos um '*' na mascara, substituimos pelo numero
        // com indice posicao em numero
        mascara[i] = numero[posicao];
        ++posicao; // incrementamos posicao
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.