func main() { text := "Yooouuuuusaaaa" fmt.Println("The old text:", text) b := []byte(text) sb := jString.NewBuffer() prevCount := 1 for i := range b { if i == 0 { sb.AppendByte(b[i]) } else { if b[i-1] != b[i] { sb.Append(strconv.Itoa(prevCount)) sb.AppendByte(b[i]) prevCount = 1 } else { prevCount++ } } } sb.Append(strconv.Itoa(prevCount)) if sb.Length() < len(text) { text = sb.ToString() } fmt.Println("The new text:", text) }
func main() { text := "This is a sentence" fmt.Println("The old text:", text) sb := jString.NewBuffer() b := []byte(text) for i := range b { if b[i] == ' ' { if i+1 != len(text) && b[i+1] != ' ' { sb.Append("%20") } } else { sb.AppendByte(b[i]) } } text = sb.ToString() fmt.Println("The new text:", text) fmt.Println("The new text using library functions:", libraryFunction(text)) }