Golang : Join lines with certain suffix symbol example
For this tutorial,we will learn how to join lines of text into a string and also explore the option to ONLY join certain lines than match a given criteria. The situation that demands this kind of join lines utility is usually involve cleanly up raw text data files. For example, in the text data files, certain lines need to be joined if some lines end with :
or +
symbols.
The code example below demonstrates how to join lines by the default empty space, insert :
symbol in between or only join the lines if a line has certain symbol as suffix.
Here you go!
package main
import (
"fmt"
"strings"
)
func JoinLines(input []string, joinBy string) string {
return strings.Join(input, joinBy)
}
func OnlyJoinLines(input []string, onlyJoinBy string) string {
var result []string
var skipIndex int
for k, v := range input {
if strings.HasSuffix(v, onlyJoinBy) {
// only join lines that match the criteria
if k+1 >= len(input) {
result = append(result, v+input[k]+"\n")
skipIndex = k
} else {
result = append(result, v+input[k+1]+"\n")
// after joining, mark the next index to skip
skipIndex = k + 1
}
} else {
if !(skipIndex > 0) {
result = append(result, v+"\n")
}
// remember to reset back to 0
skipIndex = 0
}
}
return strings.Join(result, "")
}
func main() {
lines := `line 1
line 2
line 3:
line 4
line 5:
line 6
line 7
line 8+
line 9`
// turn lines into slice
lineSlice := strings.Split(lines, "\n")
result1 := JoinLines(lineSlice, " ")
fmt.Println("RESULT 1 : ", result1)
// join all lines with :
result2 := JoinLines(lineSlice, ":")
fmt.Println("RESULT 2 : ", result2)
// ONLY join lines that have : symbol as suffix
result3 := OnlyJoinLines(lineSlice, ":")
fmt.Println("RESULT 3 : ", result3)
// ONLY join lines that have + symbol as suffix
result4 := OnlyJoinLines(lineSlice, "+")
fmt.Println("RESULT 4 : ", result4)
}
Output:
RESULT 1 : line 1 line 2 line 3: line 4 line 5: line 6 line 7 line 8+ line 9
RESULT 2 : line 1: line 2: line 3:: line 4: line 5:: line 6: line 7: line 8+: line 9
RESULT 3 : line 1
line 2
line 3: line 4
line 5: line 6
line 7
line 8+
line 9
RESULT 4 : line 1
line 2
line 3:
line 4
line 5:
line 6
line 7
line 8+ line 9
Hope this helps and happy coding!
References:
See also : Golang : Scramble and unscramble text message by randomly replacing words
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.1k Golang : Count leading or ending zeros(any item of interest) example
+15.6k Golang : Update database with GORM example
+31.7k Golang : Validate email address with regular expression
+8.5k Golang : Executing and evaluating nested loop in html template
+9.4k Golang : Copy map(hash table) example
+24.1k Golang : GORM read from database example
+11.4k Golang : Secure file deletion with wipe example
+31.1k Golang : Get local IP and MAC address
+9k Golang : Generate EAN barcode
+31.2k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+4.1k Javascript : How to show different content with noscript?
+19.4k Golang : Set or Add HTTP Request Headers