TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang Convert String to Rune Slice (append)

Convert a string into a rune slice and then call append to add a rune to it.
String, rune slice. In Golang we often use strings to store character data. But rune slices have many advantages—they treat characters in a more consistent way.Strings
For Unicode characters, a rune slice can be used to append and modify characters with no errors. If we act on a string directly, we can cause problems here.
Modify runes. A string contains runes (these are characters). We can convert a string to a slice of runes. Then we can modify runes or add new ones.

And: We create a new string from the modified rune slice. This is character-based string manipulation in Go.

Golang program that modifies runes from string package main import "fmt" func main() { original := "cat" fmt.Println(original) // Get rune slice. // ... Modify the slice's data. r := []rune(original) r[0] = 'm'; r = append(r, 'e') // Create new string from modified slice. result := string(r) fmt.Println(result) } Output cat mate
Substrings, notes. Rune slices are also ideal for acquiring substrings from a string. This supports Unicode characters with no risk of data corruption.Substring
A summary. Converting a string to a rune slice in Golang is a common operation. It leads to code that will be less likely to corrupt Unicode characters.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf