Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Working with Go String Using Example

Tags: string

This tutorial help to explore go string method. We will create a simple example to understand go string method with the example.

We will create a string and manipulate using string go method.The strings functions are stored into standard library “strings” package.A string is a read-only slice of bytes.You can get more information from Official Docs.

The string will display ugly output, if you directly output the string value into the console. You can convert string data into human readable format using fmt.println() method.
fmt.Println(sample)

What’s a String

String in Go is an immutable slice of bytes.You can create string in golang using " ".Lets look at a simple example which creates a string and prints it.

package main
import (  
    "fmt"
)

func main() {  
    name := "Hello! Restapiexample"
    fmt.Printf("%x ", name)
    fmt.Println(name)
}

The Line #8 print the Hexa character value of string and Line 9# print string into human readable form.

The program outputs:

48656c6c6f2120526573746170696578616d706c65
Hello! Restapiexample

Import Strings Package in Golang

We will import go strings packge using golang import, The syntax is as follows:

import (
	"fmt"     // for standard output
	"strings" // for manipulating strings
)

How to convert String into Uppercase and Lowercase

The golang string package have upper and lower method.The ToUpper() method used to convert string into upper case and ToLower() method convert string into lower case.

str := "RESTAPIEXAMPLE.COM"
// convert to lower case
lower := strings.ToLower(str)
fmt.Println(lower)
upper := strings.ToUpper(lower)
fmt.Println(upper)

The program outputs:

restapiexample.com
RESTAPIEXAMPLE.COM

Check String Contains Any Substring in Golang

The Golang strings package has ContainsAny() method.This ContainsAny function help to check substring is exist or not into string.The return type of this function is boolean.

If the substring found into string then return true value otherwise return false.

str := "Restapiexample.com"
if strings.ContainsAny(str, "api & hi") {
	fmt.Println("Yes, exists!")
} else {
	fmt.Println("Not, exists!")
}

Slice String into Substring in GoLang

The Golang have slice method into the array but not with strings package. You can create a substring from the string using the string index. You need to passed start index and end index of string character.

name := "Hello! Restapiexample"
fmt.Println("Characters 5-9: " + name[5:9])
fmt.Println("Characters 1-6: " + name[:6])

The program outputs:

Characters 5-9: ! Re
Characters 1-6: Hello!

How To Split String From String in Golang

The strings have split() method to create substring of string.The Split slices sting into all substrings separated by sep and returns a slice of the substrings between those separators.
If seperator not empty, Split returns a slice of length 1 whose only element is string.If seperator is empty, Split splits after each UTF-8 sequence. If both string and seperator are empty, Split returns an empty slice.

name := "Hello! Restapiexample"
words := strings.Split(name,"")
fmt.Printf("%v \n", words)
words1 := strings.Split(name,"!")
fmt.Printf("%v \n", words1[1])

The program outputs:

[H e l l o !   R e s t a p i e x a m p l e] 
Restapiexample

How To Replace String in Golang

The strings package have Replace() method to replace the charcater into the string.the replace method require three parameter, one is source string, second is old string and third is new string.the fourth is optional parameters that tell number of string replace in strings.

name := "Hello! Restapiexample"
words := strings.Replace(name,"Hello", "Hi", 1)
fmt.Printf("%v \n", words)

The program outputs:

Hi! Restapiexample

The Difference between Count and length in Golang

The Count() is the method of strings package where is len() is the basic method of golang. The Count returns the number of characters found in a string of passed character.

The len() return the length of the string.

name := "Hello! Restapiexample"
fmt.Printf("The e is found : %v time is string \n", strings.Count(name, "e"))
fmt.Printf("The length of string: %v \n", len(name))

The program outputs:

The e is found : 4 time is string 
The length of string: 21

How to Compare String in Golang

The Go strings package has the Compare () method to cpomare two strings, The return value is integer.The method Compare strings and returns an integer value – 0 if a==b, -1 if a b.

a:= "Hello! Restapiexample"
b:= "Hello! Restapiexample"
c:= "Hello! Restapiexample1"
fmt.Printf("The string compare a and b and result is: \n", strings.Compare (a, b))
fmt.Printf("The string compare a and c and result is:", strings.Compare (a, c))

The program outputs:

The string compare a and b and result is: 0
The string compare a and c and result is:-1

The post Working with Go String Using Example appeared first on Rest Api Example.



This post first appeared on Rest Api Example, please read the originial post: here

Share the post

Working with Go String Using Example

×

Subscribe to Rest Api Example

Get updates delivered right to your inbox!

Thank you for your subscription

×