Change characters within a string, in a string

3

Simple question: I just want to replace the A characters inside the " " field and not what's outside with a B, eg

aaaaaaaaaa"aaaa"aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaa"aaaaaaaaaa"aaaaaaaaaaaaa

look like this:

aaaaaaaaaa"bbbb"aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaa"bbbbbbbbbb"aaaaaaaaaaaaa

I accept responses in C # , VB and Regex .

    
asked by anonymous 16.06.2015 / 01:58

3 answers

2

You 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 a>.

    
16.06.2015 / 06:09
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();
}
    
16.06.2015 / 05:08
1

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

  • replace with \"
  • add @ at the beginning of the string and substring by ""
  • replace with the ASCII character by '\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());
}

Output:

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

MSDN Reference

    
16.06.2015 / 16:24