Removing newline from string
Asked Answered
D

7

8

Here's my issue: I have a database and it is full of episodes of a tv show. One column denotes the episode number. I want to display the episodes in a list like this:

  • Episode 1
  • Episode 2
  • Episode 3
  • etc.

I'm using my own adapter class that extends SimpleCursorAdapter to do this... Since I had formatting errors I am using Android.R.layout.simple_list_item_1 and Android.R.id.text1

Basically the only reason I have a custom adapter is so I can do something like this:

textView.setText("Episode " + cursor.getString("column_for_episode_number");

The problem is, I get a list that looks like this:

  • Episode
  • 1
  • Episode
  • 2
  • Episode
  • 3

When I try something like this(which worked in a different portion of my code):

String text = "Episode " + cursor.getString("blah");
text = text.replaceAll("\\n","");

I get the exact same list output :(

Why don't I use create a custom view with two textboxes next to each other? It is hard for me to get that to look pretty :/

Duologue answered 26/7, 2011 at 1:25 Comment(2)
What do you see if you log the values of text? I'm suspecting that your cursor is not returning what you expect.Alluring
If I log cursor.getString("blah") I get the correct number... then if I log "Episode " + cursorstuff I get the correct text (no newline shown there)Duologue
U
5

Check if there is new line at the beginning before you replace and do the same test again:

for(int i=0; cursor.getString("blah").length()-1; i++)
{

   if(cursor.getString("blah").charAt(i)=='\\n')  <-- use the constant for the line separator
    { 
      Log.i("NEW LINE?", "YES, WE HAVE");
    }

}

Or use the .contains("\n"); method:

Check the xml for the width of the textview as well.

Why are you using getString() when you are fetching an integer? Use getInt() and then use Integer.toString(theint) when you are setting the values in a textview.

Unscrew answered 26/7, 2011 at 1:35 Comment(4)
Does getString and getInt really make that big of a difference? Btw, it is the width of the textview that is the problem... nvm - figured it outDuologue
Use LayoutParams then look for the method to set the layout parameters (I forgot the name of the method) it was like textview.setLayoutParams(myLP); something like this, then add it to the layout or doit directly from the xml. set it to fill_pattern or wrap_content depends on how you want to look. DO NOT USE FIXED NUMBER OF PIXELS.Unscrew
I forgot I had copied most of the customadapter from a different one and forgot to change the layout params - thanks !Duologue
No problem. +1 for well elaborated question.Unscrew
M
12
text.replaceAll(System.getProperty("line.separator"), "");
Marquee answered 12/12, 2014 at 16:37 Comment(0)
E
7

There is a mistake in your code. Use "\n" instead of "\\n"

String myString = "a string\n with new line"
myString = myString.replaceAll("\n","");
Log.d("myString",myString);
Electrojet answered 7/11, 2016 at 7:17 Comment(0)
U
5

Check if there is new line at the beginning before you replace and do the same test again:

for(int i=0; cursor.getString("blah").length()-1; i++)
{

   if(cursor.getString("blah").charAt(i)=='\\n')  <-- use the constant for the line separator
    { 
      Log.i("NEW LINE?", "YES, WE HAVE");
    }

}

Or use the .contains("\n"); method:

Check the xml for the width of the textview as well.

Why are you using getString() when you are fetching an integer? Use getInt() and then use Integer.toString(theint) when you are setting the values in a textview.

Unscrew answered 26/7, 2011 at 1:35 Comment(4)
Does getString and getInt really make that big of a difference? Btw, it is the width of the textview that is the problem... nvm - figured it outDuologue
Use LayoutParams then look for the method to set the layout parameters (I forgot the name of the method) it was like textview.setLayoutParams(myLP); something like this, then add it to the layout or doit directly from the xml. set it to fill_pattern or wrap_content depends on how you want to look. DO NOT USE FIXED NUMBER OF PIXELS.Unscrew
I forgot I had copied most of the customadapter from a different one and forgot to change the layout params - thanks !Duologue
No problem. +1 for well elaborated question.Unscrew
L
2

This could help you:

response = response.replaceAll("\\s+","");
Linotype answered 3/11, 2017 at 5:27 Comment(0)
J
0

It sounds like you are hitting wrapping issues rather than newline issues. Change this:

String text = "Episode " + cursor.getString("blah");

To this:

String text = "Episode" + cursor.getString("blah");

And see if that changes the output. Post your layout xml please?

Jonis answered 26/7, 2011 at 1:45 Comment(0)
C
0

this worked for my (on android 4.4):

(where body is a string with a newline entered from an EditText view on handset)

for (int i=0; i<body.length(); i++) {
    if (body.charAt(i) == '\n' || body.charAt(i) == '\t') {
        body = body.substring(0, i) + " " + body.substring(i+1, body.length());                         
    }
}
Cockspur answered 7/8, 2014 at 20:22 Comment(0)
I
-1

have you tried cursor.getString("blah").trim()

Illogical answered 18/10, 2013 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.