Heim > Artikel > Backend-Entwicklung > Wie entferne ich Zeilenumbrüche aus aus einer Textdatei gelesenen Zeilen?
Removing Newline Characters from Lines
In your code, you are using ReadString() to read lines from a text file. However, this function adds a newline character (\n) to the end of each line it reads. This can cause issues when processing the lines, as the newline character may need to be removed.
To remove the newline character from the end of each line, you can use the following approach:
Option 1: String Slicing
read_line = read_line[:len(read_line)-1]
This line of code slices the read_line string, removing the last character (which is the newline character).
Option 2: Strings Library
read_line = strings.TrimSuffix(read_line, "\n")
The TrimSuffix() function from the strings library removes the specified suffix from the end of a string. In this case, the suffix to be removed is the newline character (\n).
Both of these approaches will effectively remove the newline character from the end of each line, allowing you to work with the parsed lines without any issues caused by the presence of a trailing newline character.
Das obige ist der detaillierte Inhalt vonWie entferne ich Zeilenumbrüche aus aus einer Textdatei gelesenen Zeilen?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!