Swap characters inside a string, in a string

Asked

Viewed 3,527 times

3

Simple question: I just want to replace the characters A inside the field " " and not what is out for a B, example:

aaaaaaaaaa"aaaa"aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaa"aaaaaaaaaa"aaaaaaaaaaaaa

stay that way:

aaaaaaaaaa"bbbb"aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaa"bbbbbbbbbb"aaaaaaaaaaaaa

I accept answers in C#, VB and Regex.

3 answers

2


Can use regex, as follows:

C#

string original = @"aaaaaaaaaa""aaaa""aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa""aaaaaaaaaa""aaaaaaaaaaaaa";

// percorre todas os valores que estejam entre aspas duplas
foreach (Match match in Regex.Matches(original, @"\""+[A-z].*?\""", RegexOptions.IgnoreCase))
{
    // pega o valor encontrado e remove as aspas
    string valorEncontrado = match.Value.Replace("\"", String.Empty);
    // cria uma nova string do tamanho do valor encontrado
    string novoValor = new String('b', valorEncontrado.Length);
    // substitui o valor original pela nova string, re-adicionando as aspas
    original = original.Replace(match.Value, "\"" + novoValor + "\"");
}

VB

Dim original As String = "aaaaaaaaaa""aaaa""aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa""aaaaaaaaaa""aaaaaaaaaaaaa"

For Each match As Match In Regex.Matches(original, """[A-z].*?""", RegexOptions.IgnoreCase)
    Dim valorEncontrado As String = match.Value.Replace("""", String.Empty)     
    Dim novoValor As String = New String("b"c, valorEncontrado.Length)
    original = original.Replace(match.Value, """" + novoValor + """")
Next

See working here in C# and here in VB.

1

Hello, you can use this code:

string a = "aaaaaaaaaa\"aaa\"aaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaa\"aaaaaaaaaa\"aaaaaaaaaaaaa";
string[] nova = a.Split('"');
for (int i = 0; i < nova.Length; i++)
{
    if (i==1 || i%3==0)
    {
        int x = nova[i].Length;
        StringBuilder k = new StringBuilder();
        for (int v = 0; v < x; v++)
        {
            k.Append("b");
        }
        nova[i] = '"'+k.ToString()+'"';
    }
}
string novastring;
foreach (var item in nova)
{
    novastring += item.ToString();
}
  • The your code is returning "bbbbbbbbbb""bbb"aaaaaaaaaaaaaa&#xA;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#xA;aaaaa"bbbbbbbbbb"aaaaaaaaaaaaa

  • if (i==1 || i%3==0 && i != 0)

  • changes the if statement, leaves it so

  • 1

    I also recommend avoiding the regex as much as possible, because it is very complicated.

1

The characters " in the middle of the string are not accepted as in the question. To add a " in the middle of a string should be:

  • replace by \"
  • add @ at the beginning of the string and replace with ""
  • replace by the ASCII character with '\u0022'

Follow the code in c#:

List<string> listaOriginal = new List<string>();
List<string> listaFinal = new List<string>();

listaOriginal.Add("aaaaaaaaaa\"aaaa\"aaaaaaaaaaaaaa");
listaOriginal.Add("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listaOriginal.Add("aaaaa\"aaaaaaaaaa\"aaaaaaaaaaaaa");

foreach (string original in listaOriginal)
{
    StringBuilder final = new StringBuilder();

    string[] split = original.Split('\"');

    final.Append(split[0]);

    for (int i = 1; i < split.Length; i++)
    {
        if (i % 2 != 0)
        {
            string s = split[i].Replace("a", "b");
            split[i] = s;
        }

        final.Append("\"" + split[i]);
    }

    listaFinal.Add(final.ToString());
}

Exit:

listaFinal
Count = 3
[0]: "aaaaaaaaaa\"bbbb\"aaaaaaaaaaaaaa"
[1]: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
[2]: "aaaaa\"bbbbbbbbbb\"aaaaaaaaaaaaa"

MSDN reference

Browser other questions tagged

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