Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing newline from string

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 :/

like image 764
IamAlexAlright Avatar asked Jul 26 '11 01:07

IamAlexAlright


People also ask

Does Rstrip remove newline?

rstrip('\n') . This will strip all newlines from the end of the string, not just one.

How do you remove a new line character from a string in Java?

Line Break: A line break (“\n”) is a single character that defines the line change. In order to replace all line breaks from strings replace() function can be used.


2 Answers

text.replaceAll(System.getProperty("line.separator"), "");
like image 160
thecoolmacdude Avatar answered Oct 07 '22 01:10

thecoolmacdude


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.

like image 26
Nikola Despotoski Avatar answered Oct 07 '22 00:10

Nikola Despotoski