-1

I am trying to remove\replace last line of a TextView, but I want a way to do this faster. For example I found this:

String temp = TextView.getText().toString();
temp.substring(0,temp.lastIndexOf("\n"));

But I want to do it faster without copy data from Textview to a string value and dont using Sting.lastIndexOf (because its search in a string).

Can some one help me?

My Question isn't a dupplicate question becuase I want a way without using a string type!!!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Amir133
  • 2,372
  • 2
  • 18
  • 34
  • Possible duplicate of [How to remove newlines from beginning and end of a string (Java)?](https://stackoverflow.com/questions/7454330/how-to-remove-newlines-from-beginning-and-end-of-a-string-java) – NSimon Jun 27 '18 at 14:23
  • 2
    So if I understand correctly your current way does work, but you want a simpler alternative? One question: do you want to remove the last line of a String or of the TextView itself? If the first, your method seems fine by me (perhaps put it in a method with a `TextView` as parameter for re-usability). If the second, you currently use `TextView.setText(temp)` I assume? You could perhaps overwrite the `TextView` class and create a method `removeLastLine()` with the code you currently have. – Kevin Cruijssen Jun 27 '18 at 14:31
  • yes my question is second one , But I dont want to copy its data to an String – Amir133 Jun 27 '18 at 14:34
  • My Question isn't a dupplicate question becuase I want a way without using a string type – Amir133 Jun 27 '18 at 14:43
  • 1
    But why? `TextView` doesn't have a builtin for `removeLastLine()`, so you'll either have to create a child-class with your own implementation overwriting the existing `TextView` and containing an additional `removeLastLine()` which you can use. Or if you are looking for a one-liner without creating a variable for `String temp` for whatever reason, you could use `textView().setText(textView().getText().toString().replaceAll("(.*)\n[^\n]+$", "$1"));` for example.. I just don't understand ***why*** you don't want the `String temp = ...` :S I've voted to close as 'unclear'.. – Kevin Cruijssen Jun 27 '18 at 14:47
  • because my program is very large and I want a very fast program so when I use `string.replace()` or `string.lastIndex()` , its search my String and my program run slower.So I want to do it with a better way. – Amir133 Jun 27 '18 at 15:05
  • amir133, I hope you do understand that removing any part of a string involves: (a) finding that part, and (b) removing it. So how do you expect to do it without `lastIndex` and `substring` –  Jun 27 '18 at 16:57

3 Answers3

3

I suggest overwriting the TextView class with your own implementation:

public class CustomTextView extends TextView{
  // Overwrite any mandatory constructors and methods and just call super

  public void removeLastLine(){
    if(getText() == null) return;
    String currentValue = getText().toString();
    String newValue = currentValue.substring(0, currentValue.lastIndexOf("\n"));
    setText(newValue);
  }
}

Now you could use something along the lines of:

CustomTextView textView = ...

textView.removeLastLine();

Alternatively, since you seem to be looking for a one-liner without creating a String temp for some reason, you could do this:

textView.setText(textView.getText().toString().replaceFirst("(.*)\n[^\n]+$", "$1"));

Regex explanation:

(.*)            # One or more character (as capture group 1)
    \n          # a new-line
      [^\n]     # followed by one or more non new-lines
           $    # at the end of the String

$1              # Replace it with the capture group 1 substring
                # (so the last new-line, and everything after it are removed)

Try it online.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • 1
    because my program is very large and I want a very fast program so when I use `string.replace()` or `string.lastIndex()` , its search my String and my program run slower.So I want to do it with a faster way. – Amir133 Jun 27 '18 at 15:16
0

Try this:

public String removeLastParagraph(String s) {
    int index = s.lastIndexOf("\n");
    if (index < 0) {
        return s;
    }
    else {
        return s.substring(0, index);
    }
}

and use it like:

tv.setText(removeLastParagraph(tv.getText().toString().trim());
-1

Use the System.getProperty("line.seperator") instead of "\n"

public void removeLastLine(TextView textView) {
    String temp = textView.getText().toString();
    textView.setText(
        temp.substring(0, temp.lastIndexOf(System.getProperty("line.seperator") )));
}
CodeChimp
  • 4,745
  • 6
  • 45
  • 61