Jump to content
m0fl

Substituir "," que se encontram entre " "" "

Recommended Posts

m0fl

Boas!

Tenho aqui uma duvida ..

Tenho um ficheiro de texto, faço o split do ficheiro por virgula mas tenho texto no ficheiro que esta entre aspas. E esse mesmo texto entre aspas contem tambem virgulas mas nesse caso nao seria para fazer o split.

Será possivel ignorar o split em texto que se encontre entre aspas duplas ?

aaaa,bbb,cccc,"adsda,asdasda",ddddd

Ignorar a virgula que esta em "adsda,asdasda".

Obrigado ;)

Share this post


Link to post
Share on other sites
ribeiro55

Só com o split não.

A vírgula entre aspas terá de ser transformada num equivalente antes de fazer o split.

Por exemplo, à pressão e sem expressões regulares:

        Dim S As String = "aaaa,bbb,cccc,""adsda,asdasda"",ddddd"
       Dim saida As String = String.Empty
       Dim zona As Boolean = False
       For p As Integer = 0 To S.Length - 1
           If S(p) = Chr(34) Then zona = Not zona
           If zona And S(p) = "," Then saida &= ";virg;" Else saida &= S(p)
       Next
       Dim Elementos As String() = saida.Split(",")
       For i As Integer = 0 To Elementos.Length - 1
           Elementos(i) = Elementos(i).Replace(";virg;", ",").Trim(Chr(34))
       Next
       'Elementos é um array com todos os teus elementos íntegros

Edited by ribeiro55

Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.