![]() ![]() Which replaces each line terminator with a space 1. So you might actually want to do this: // #4 However, if the input file is text, this will concatenate words e.g. Removes all Windows, UNIX or Mac line terminators. This does not cope with the case where you are trying to process (for example) a UNIX file on Windows, or vice versa. Removes all line terminators for the current platform. Text = text.replace(System.getProperty("parator"), "") This does not cope with Windows or Mac line terminations. ![]() Simply removes all the newline characters. Which one is most correct depends on what exactly you are trying to do. But your code then throws away that String. (It can't - Java strings are immutable!) What replace actually does is to create and return a new String object with the characters changed as required. If you don't assign the result to text, then that new String is lost and garbage collected.Īs for getting the newline String for any environment - that is available by calling System.getProperty("parator").Īs noted in other answers, your code is not working primarily because String.replace(.) does not change the target String. This is necessary because Strings are immutable - calling replace doesn't change the original String, it returns a new one that's been changed. You need to set text to the results of text.replace(): String text = readFileAsString("textfile.txt") ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |