# Removing the last line of a String

**URL:** <https://forums.visual-paradigm.com/t/removing-the-last-line-of-a-string/10547>\
**Category:** Uncategorized\
**Created:** [October 25, 2008, 4:56pm UTC](https://forums.visual-paradigm.com/t/removing-the-last-line-of-a-string/10547 "2008-10-25T16:56:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anonymous](https://avatars.discourse-cdn.com/v2/letter/a/8491ac/32.png) [@Anonymous](https://forums.visual-paradigm.com/u/Anonymous)\
**Post date:** [October 25, 2008, 4:56pm UTC](https://forums.visual-paradigm.com/t/removing-the-last-line-of-a-string/10547/1 "2008-10-25T16:56:04Z")

</div>

Hey guys

This is probably a very simple question, but i can’t seem to get the just of it.

String sentence = “Hello\nWorld”;  
now i want to remove the last line of the string ("\nWorld");

i.e. sentence -= "\n"  
but then it says  
"operator - cannot be applied to java.lang.String,java.lang.String"

---

<div class="post-metadata">

**Author:** ![Anonymous](https://avatars.discourse-cdn.com/v2/letter/a/8491ac/32.png) [@Anonymous](https://forums.visual-paradigm.com/u/Anonymous)\
**Post date:** [November 30, 2008, 11:54pm UTC](https://forums.visual-paradigm.com/t/removing-the-last-line-of-a-string/10547/2 "2008-11-30T23:54:26Z")

</div>

indexOf and substring

---

<div class="post-metadata">

**Author:** ![ShelLuser](https://forums.visual-paradigm.com/user_avatar/forums.visual-paradigm.com/shelluser/32/204_2.png) [@ShelLuser](https://forums.visual-paradigm.com/u/ShelLuser)\
**Post date:** [March 30, 2009, 8:32am UTC](https://forums.visual-paradigm.com/t/removing-the-last-line-of-a-string/10547/3 "2009-03-30T08:32:09Z")

</div>

I somewhat disagree with the previous answer. You could combine indexOf() and substring() but I think split() is much better suited since you will get a String[] array containing all the parts you were after.

Naturally this heavily depends on what the final goals are but since that wasn’t included…
