How to split a string in half but not in the middle of a word with Go

1 Answer

0 votes
package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "c# c java c++ python go rust"

	halfstr := str[:len(str) / 2 + 1]
	center := strings.LastIndex(halfstr, " ") + 1

	parts := []string{str[:center], str[center:]}

	fmt.Println(parts[0])
	fmt.Println(parts[1])
}


/*
run:

c# c java c++ 
python go rust

*/

 



answered Sep 10, 2024 by avibootz
edited Sep 10, 2024 by avibootz
...