Posts

Showing posts with the label String

How to iterate over all the characters of the string in Go Language

In Go Language, a string is a slice of bytes. The bytes of the strings can be defined in the Unicode text using UTF-8 encoding. UTF-8 is based on 8-bit code units. Each character is encoded as 1 to 4 bytes. The first 128 Unicode code points are encoded as 1 byte in UTF-8. For Example, package main import "fmt" func main() { text := "aπ" fmt.Println(len(text)) } Output: 3 Here the length of the String is 3 because, ‘a’ takes one byte as per UTF-8  ‘π’ takes two bytes as per UTF-8 So we cannot use for loop to iterate over all the characters of the string because  it will iterate over bytes and not characters. This means  it will iterate three times and the print value corresponding to a byte at that index. How to iterate over all the characters of the string in Go Language? Iterate over a string by runes  Iterate over a string by for-range loop Iterate over a string by runes Go allows us to easily convert a string to a slice of runes and then iterate over t

Trim a string in Go Language - Examples

Image
Hello everyone, In this tutorial, we will show you how to trim all of the leading and trailing spaces in a string. Few methods from the strings package are, Trim():  TrimSpace() TrimLeft() TrimRight() TrimPrefix() TrimSuffix() Example 1:  strings.Trim() function The Trim() function is an inbuilt method of the strings package. This method  will remove the leading and trailing whitespace from a string that is received as a parameter. // Golang program to demonstrate the // example of strings.Trim() Method package main import ( "fmt" "strings" ) func main() { str1 := " My world is too small " fmt.Println( "Before:" , str1) str4 := strings.Trim(str1, " " ) fmt.Println( "After:" , str4) } Output: Example 2:  strings.TrimSpace() function The TrimSpace() function is an inbuilt method of the strings package. This is a better choice if a variety of whitespace runes may be present at the start or end of a

How to check if a String is an Integer in Go Language

Image
Hello everyone, in this tutorial, we’ll explore multiple ways to detect if the given String is an int. Example 1: Using strconv.Atoi() function package main import ( "fmt" "strconv" ) func main() { str1 := "5674" str2 := "123o" fmt.Println(IsInt(str1)) fmt.Println(IsInt(str2)) } func IsInt(str string ) bool { _, err := strconv.Atoi(str) if err != nil { return false } else { return true } } Output: Author: Joy More Related Topics, How to make a delay in Go Language Program to check if a string contains the substring How to Split a String by Space in Go Language? Trigonometry with Go - Sin, Cos, Tan Functions Examples How to iterate over a map in Go? Go Arrays - Program to Find Largest & Smallest Element Go Arrays - Program to find the duplicate elements of an Array Go - Factorial Program using loop Go - Palindrome Program Example Go Program to print Odd and Even Numbers from an

Go Language, Program to check if a string contains substring

Image
Go language provides a powerful strings package that holds different types of methods to manipulate UTF-8 encoded strings. How to check if a string contains a substring in Go Language String Contains() function checks whether the substring is within the string. The Contains() function accepts two arguments and returns the boolean value, either true or false. package main import ( "fmt" "strings" ) func main() { str := "HelloWorld Example" fmt.Println(strings.Contains(str, "World" )) fmt.Println(strings.Contains(str, "exam" )) fmt.Println(strings.Contains(str, "Hell" )) } Output: How to check if a string contains  any chars in the provided string. package main import ( "fmt" "strings" ) func main() { str := "HelloWorld Example" fmt.Println(strings.ContainsAny(str, "moon" )) fmt.Println(strings.ContainsAny(str, "Fun" )) fmt

How to make the first letter of a string lowercase in Go Language?

Image
How to make the first letter of a string lowercase, but not change the case of any of the other letters? For example: "Go language is awesome" → "go language is awesome" "Hello world" → "hello world" Solution 1:  package main import ( "unicode" "unicode/utf8" ) // Main function func main() { str := "Go language is awesome" println(firstToLower(str)) } func firstToLower(s string ) string { if len(s) > 0 { r, size := utf8.DecodeRuneInString(s) if r != utf8.RuneError || size > 1 { lower := unicode.ToLower(r) if lower != r { s = string(lower) + s[size:] } } } return s } Output: Solution 2: Easy package main import "strings" // Main function func main() { str := "Go language is awesome" str = strings.ToLower(string(str[ 0 ])) + str[ 1 :] println(str) } Output: Solution

How to make the first letter of a string uppercase in Go Language?

Image
How to make the first letter of a string uppercase, but not change the case of any of the other letters? For example: "go language is awesome" → "Go language is awesome" "hello world" → "Hello world" Solution 1: Easy package main import ( "unicode" ) // Main function func main() { str := "go language is awesome" runes := []rune(str) if len(runes) > 0 { runes[ 0 ] = unicode.ToUpper(runes[ 0 ]) } println(string(runes)) } Output: Solution 2: Easy package main import "strings" // Main function func main() { str := "go language is awesome" str = strings.ToUpper(string(str[ 0 ])) + str[ 1 :] println(str) } Output: Solution 3: Normal package main import ( "unicode" "unicode/utf8" ) // Main function func main() { str := "go language is awesome" println(firstToUpper(str)) } func firstToUpper(s string ) string {