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

Some Common String Problems & Solution of Golang

This golang tutorial help to understand go String package.This tutorial apply some manipulation and modify string using go string.The Package strings implements simple functions to manipulate UTF-8 encoded strings. We will import ‘string’ package for string manipulation.

We will here discuss some some common string operation using Go.Lets start example of string function.

How to Get Length of String in Golang

We will use len() function to get string length but its not string package method.The string length will start from 0.

s := "Hello! I am restapiexample team."
l := len(s)
fmt.Println(l)

//output : 19

How to Split String in Golang

We will use string package method Split("source", "delimeter"), That have two parameters one is source string, other is delimiter. This will return array of split strings.

s := "Hello! I am restapiexample team."
l := strings.Split(s," ")
fmt.Println(l[2])
//output : am

How to get Last index of array

We will get find last index of array and get data using go.

s := "Hello! I am restapiexample team."
l := strings.Split(s," ")
last_index := len(l)
fmt.Println(l[last_index-1])
//output : team

How to Split string Using RegEX in Golang

Golang has regex package for regular expression manipulation, We will use Split() function to split string based on regular expression in golang.

import (
	"fmt"
	"regexp"
)

func main() {
s := "Hello! I am 1 restapiexample 2 team."
a := regexp.MustCompile("[0-9]+")
st := a.Split(s, -1)

fmt.Print(st[1])
}
//output : restapiexample

How to check sub string exist in String Using Golang

We will use Contains() method of string package to check sub-string exist.

s := "Hello! I am restapiexample team."
l := s.Contains(s, "am")
fmt.Println(l)
//output : true

How to get index of character in String Using Golang

We will use Index() method of string package to check sub-string first occurrence in a string.

s := "Hello! I am restapiexample team."
l := strings.Index(s, "am")

fmt.Println(l)
//output : 9

How to Join Strings into single String Using Golang

We will use join() method of string package to create single string using array of strings.

s := []string {"Hello!", "I", "am", "restapiexample", "team."}
l := strings.Join(s, " ")

fmt.Println(l)

//output : Hello! I am restapiexample team.

How to Replace string Using Golang

We will use Replace(source+string, old, new, n) method of string package to replace string into source string, the n is the no of string will replace, If n , There is no limit on the number of replacements.

s := "Hello! I am restapiexample team."
l := strings.Replace(s, "e", "x", 1)
l1 := strings.Replace(s, "e", "x", -1)

fmt.Println(l)
fmt.Println(l1)
//output : 
//l : Hxllo! I am restapiexample team.
//l1 : Hxllo! I am rxstapixxamplx txam.

How to convert string into Lower Charcater

We will use ToLower() method to converts string into lowercase in golang.

s := "Hello! I am restapiexample team."
l := strings.ToLower(s)

fmt.Println(l)
//output : hello! i am restapiexample team.

How to convert string into Upper Charcater

We will use ToUpper() method to converts string into uppercase in golang.

s := "Hello! I am restapiexample team."
l := strings.ToUpper(s)

fmt.Println(l)
//output : HELLO! I AM RESTAPIEXAMPLE TEAM.

The post Some Common String Problems & Solution of Golang appeared first on Rest Api Example.



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

Share the post

Some Common String Problems & Solution of Golang

×

Subscribe to Rest Api Example

Get updates delivered right to your inbox!

Thank you for your subscription

×