Split(), using | (pipe) as separator, does not correctly separate the text

Asked

Viewed 1,109 times

3

I’m developing an app for android and now did the part of Httpurlconnection, I put everything right, I pointed out url and took a test with System.out.printl(line) and on the console prints my string with all data starting from my hosted php file.

private class MyAsyncTask extends  AsyncTask
{
    @Override
    protected Object doInBackground(Object[] params)
    {
        HttpURLConnection conection;
        try
        {
            //Configuração de conexao
            URL url = new URL("http://prinxel.esy.es/horoscopo.php");
            conection = (HttpURLConnection) url.openConnection();
            conection.connect();

            // Lendo os dados

            InputStream inputStream = conection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();

            //Storing data

            String line = new String();

            while ((line = bufferedReader.readLine())!= null)
            {
                System.out.println(line); // imprime todos os dados certinhos
                textoSeparado = line.split("|"); 
            }

            //Close Conection
            inputStream.close();
            conection.disconnect();

        }

        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }

        catch (IOException e)
        {
            e.printStackTrace();
        }


        return  null;
    }

    @Override
    protected void onPostExecute(Object o)
    {
        super.onPostExecute(o);
    }
}

However, when I use the split to separate items from my string, nothing appears on my console.
I want to divide to put in each text view a message from my array which will be formed after the split of the variable line.

Content of the variable line

|Gêmeos vai sentir-se com algumas limitações que serão criadas apenas pela sua cabeça já que é um mês em que terá progresso e evolução.|Câncer estará bastante empenhado e concentrado no seu trabalho podendo esquecer-se por vezes da sua vida afetiva.|Leão terá um mês positivo em que poderá obter a realização que deseja em vários setores da sua vida.|

ps: I have tried to do line.split("|") outside the while and it didn’t work

  • How many lines are read?

  • Only one, the line contains all my data. When I split using split I would have an array of 11 elements, 0,1,2...

1 answer

4


split() receives as parameter a regex, as | has a special function(is a meta-character) in regex it is not being considered as separator character.

To ensure that the | is properly interpreted needs to be "escaped":

textoSeparado = line.split("\\|");

Another way to treat metacharacters is to use Pattern quote.()

textoSeparado = line.split("Pattern.quote("|"));
  • Thank you very much, I understood how it works and solved my problem.

  • Very good +1. = D

Browser other questions tagged

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