string Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/string/ Tue, 06 Apr 2021 12:40:40 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 string Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/string/ 32 32 159787465 Iterate over a string in Go (Golang) https://vikasboss.github.io/iterate-over-a-string-golang/ https://vikasboss.github.io/iterate-over-a-string-golang/#respond Sat, 20 Jun 2020 15:42:35 +0000 https://vikasboss.github.io/?p=2322 In Golang string is a sequence of bytes. A string literal actually represents a UTF-8 sequence of bytes. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. All other...

The post Iterate over a string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
In Golang string is a sequence of bytes. A string literal actually represents a UTF-8 sequence of bytes. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. All other characters are between 1 -4 bytes. To understand it more consider the below string

sameple := "a£c"

In above string

  • ‘a’ takes one byte as per UTF-8
  • ‘£’ takes two bytes as per UTF-8
  • ‘b’ takes one byte as per UTF-8

The above string has 1+2+1 = 4 bytes altogether. Therefore when we try to print the length of the string using the standard len() function it will output 4 and not 3 as len() function returns the number of bytes in the string.

fmt.Printf("Length is %d\n", len(sample))

Hence standalone for loop cannot be used to iterate over all characters of a string as it will iterate over bytes and not character. So below for loop will instead iterate four times and the print value corresponding to a byte at that index.

 for i := 0; i < len(sample); i++ {
    fmt.Printf("%c\n", sample[i])
 }

It will output below string which is not same as sample string

a£b

Not we have mentioned the above limitation of using len() function and for loop, let's see two ways of calculating the length of the string.

  • Using for-range loop
  • By converting string to a rune array.

Using for-range loop

for-rage iterates over the Unicode points( also referred to as rune in golang) in a string and will correctly output  a, £, b. Hence it can also be used to calculate the length of the string. Here is the format when using for-range with string

for index, character := range string {
    //Do something with index and character
}

Sample code

package main

import "fmt"

func main() {
    sample := "a£b"
  
    for i, letter := range sample {
        fmt.Printf("Start Index: %d Value:%s\n", i, string(letter))
    }
}

Output

Start Index: 0 Value:a
Start Index: 1 Value:£
Start Index: 3 Value:b

By converting string to rune array

A rune represents a Unicode Point. By converting a string to rune array basically it is same as creating an array of Unicode Points of that string. Therefore once the string is converted into the rune array, it can be used to iterate over all characters of the string.

package main

import "fmt"

func main() {
    sample := "a£b"
    runeSample := []rune(sample)
    fmt.Printf("Length of given string is %d\n", len(runeSample))
    //Iterate
    for i := 0; i < len(runeSample); i++ {
        fmt.Println(string(runeSample[i]))
    }
}

Output

Length of given string is 3
a
£
b

The post Iterate over a string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/iterate-over-a-string-golang/feed/ 0 2322
Length of string in Go (Golang) https://vikasboss.github.io/length-of-string-golang/ https://vikasboss.github.io/length-of-string-golang/#comments Sat, 20 Jun 2020 15:31:04 +0000 https://vikasboss.github.io/?p=2317 In Golang string is a sequence of bytes. A string literal actually represents a UTF-8 sequence of bytes. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. All other...

The post Length of string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
In Golang string is a sequence of bytes. A string literal actually represents a UTF-8 sequence of bytes. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. All other characters are between 1 -4 bytes. To understand it more consider the below string

sample := "a£c"

In above string

  • ‘a’ takes one byte as per UTF-8
  • ‘£’ takes two bytes as per UTF-8
  • ‘b’ takes one byte as per UTF-8

The above string has 1+2+1 = 4 bytes altogether. Therefore when we try to print the length of the string using the standard len() function it will output 4 and not 3 as len() function returns the number of bytes in the string.

fmt.Printf("Length is %d\n", len(sample))

Hence standalone for loop cannot be used to iterate over all characters of a string as it will iterate over bytes and not character. So below for loop will instead iterate four times and the print value corresponding to a byte at that index.

for i := 0; i < len(sample); i++ {
    fmt.Printf("%c\n", sample[i])
 }

It will output below string which is not same as sample string

a£b

Not we have mentioned the above limitation of using len() function and for loop, let's see two ways of calculating the length of the string.

  • Using the RuneCountInString method of the utf8 package
  • Using for-range loop
  • By converting string to a rune array.

Using the RuneCountInString method of the utf8 package

utf8 package of golang provides a RuneCountInString method that can be used to get the length of the string. It correctly counts the number of runes in the string.

https://golang.org/pkg/unicode/utf8/#RuneCountInString

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	sample := "a£b"

	fmt.Printf("Length of given string is %d\n", utf8.RuneCountInString(sample))
}

Output

Length of given string is 3

Using for-range loop

for-range iterates over the Unicode points( also referred to as rune in golang) in a string and will correctly output  a, £, b. Hence it can also be used to calculate the length of the string. Here is the format when using for-range with string

for index, character := range string {
    //Do something with index and character
}

Sample code

package main

import "fmt"

func main() {
	sample := "a£b"

	len := 0
	//With index and value
	fmt.Println("Both Index and Value")
	for i, letter := range sample {
		len++
		fmt.Printf("Start Index: %d Value:%s\n", i, string(letter))
	}

	fmt.Printf("Length of given string is %d\n", len)
}

Output

Start Index: 0 Value:a
Start Index: 1 Value:£
Start Index: 3 Value:b
Length of given string is 3

By converting string to rune array

A rune represents a Unicode Point. By converting a string to rune array basically it is same as creating a array of Unicode Points of that string. Therefore once the string is converted into the rune array, it can be used to iterate over all characters of the string.

package main

import "fmt"

func main() {
	sample := "a£b"

	runeSample := []rune(sample)

	fmt.Printf("Length of given string is %d\n", len(runeSample))
}

Output

Length of given string is 3

The post Length of string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/length-of-string-golang/feed/ 3 2317
Generate a random string in Go (Golang) https://vikasboss.github.io/generate-random-string-golang/ https://vikasboss.github.io/generate-random-string-golang/#respond Wed, 08 Apr 2020 11:25:23 +0000 https://vikasboss.github.io/?p=1947 Overview ‘mat/rand’ package of golang contains a Intn function that can be used to generate a pseudo-random number between [0,n). Bracket at the end means that n is exclusive. This function can...

The post Generate a random string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

‘mat/rand’ package of golang contains a Intn function that can be used to generate a pseudo-random number between [0,n). Bracket at the end means that n is exclusive. This function can be utilized to generate a random string from a character set.


To know more about what pseudo-random number means, checkout this post – https://vikasboss.github.io/generate-random-number-golang

Below is the signature of this method. It takes input a number n and will return a number x in range 0<=x<n.

func Intn(n int) int

Above function can be used to generate a random string. Basically we first select a charSet. Then we use the above function to generate a random number and then use that random number to get a random character from the charSet. This random character is added to a string until we have a random string of desired length.

Code

package main

import (
    "fmt"
    "math/rand"
    "strings"
    "time"
)

func main() {
    rand.Seed(time.Now().Unix())

    //Only lowercase
    charSet := "abcdedfghijklmnopqrst"
    var output strings.Builder
    length := 10
    for i := 0; i < length; i++ {
        random := rand.Intn(len(charSet))
        randomChar := charSet[random]
        output.WriteString(string(randomChar))
    }
    fmt.Println(output.String())
    output.Reset()

    //Lowercase and Uppercase Both
    charSet = "abcdedfghijklmnopqrstABCDEFGHIJKLMNOP"
    length = 20
    for i := 0; i < length; i++ {
        random := rand.Intn(len(charSet))
        randomChar := charSet[random]
        output.WriteString(string(randomChar))
    }
    fmt.Println(output.String())
}

Output:

Below is the output on my machine. On your's it might give a different output

himsemkpkd
nHaiEpccEdBfCFPtaBbi

In above program we are using character set as

abcdedfghijklmnopqrst and abcdedfghijklmnopqrstABCDEFGHIJKLMNOP

All the above characters in charSet were ASCII characters hence we were able to index a character in charSet string. But this could be problem if the charSet contains non-ASCII character.

In Golang string is a sequence of bytes. A string literal actually represents a UTF-8 sequence of bytes. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. All other characters are between 1 to 4 bytes. Due to this it is not possible to index a character in a string.  In GO, rune data type represents a Unicode point.  Once a string is converted to an array of rune then it is possible to index a character in that array of rune.

So in case, the character set contains some characters that are not ASCII they might occupy more than 1 bytes. In that case, we cannot use the above code to generate a random string as we cannot index into the charSet. For this case, we have to first convert a string into a rune array so that we can index into the rune array to the character and then incrementally form the random string.

As in the below example, our charSet contains a non-ASCII character '£'. This character occupies two bytes

package main

import (
    "fmt"
    "math/rand"
    "strings"
    "time"
)

func main() {
    rand.Seed(time.Now().Unix())
    //Only lowercase and £
    charSet := []rune("abcdedfghijklmnopqrst£")
    var output strings.Builder
    length := 10
    for i := 0; i < length; i++ {
        random := rand.Intn(len(charSet))
        randomChar := charSet[random]
        output.WriteRune(randomChar)
    }
    fmt.Println(output.String())
    output.Reset()
 
   //Lowercase and Uppercase Both and £
    charSet = []rune("abcdedfghijklmnopqrstABCDEFGHIJKLMNOP£")
    length = 20
    for i := 0; i < length; i++ {
        random := rand.Intn(len(charSet))
        randomChar := charSet[random]
        output.WriteRune(randomChar)
    }
    fmt.Println(output.String())
}

Output:

Below is the output on my machine. On yours it might give a different output

aidqpbse£j
rebhjblsePsLpGBPOhfB

The post Generate a random string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/generate-random-string-golang/feed/ 0 1947
Shuffle a string in Go (Golang) https://vikasboss.github.io/shuffle-string-golang/ https://vikasboss.github.io/shuffle-string-golang/#respond Thu, 02 Apr 2020 06:45:12 +0000 https://vikasboss.github.io/?p=1921 Overview math/rand package of GO provides a Shuffle method that can be used to shuffle a string. This method pseudo-randomizes the order of elements using the default source. pseudo-randomizes means that for...

The post Shuffle a string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

math/rand package of GO provides a Shuffle method that can be used to shuffle a string. This method pseudo-randomizes the order of elements using the default source. pseudo-randomizes means that for a fixed input seed it will generate the same randomization. That is why in our program we will initialize the rand package with a different seed every time.

Below is the signature of the function.

func Shuffle(n int, swap func(i, j int))

This function takes in below arguments

  • First is the total number of characters in the given string.
  • The second is a swap function that will be called for different indexes i and j. You need to provide your own swap function that will swap your elements in the string.

Also note that this function will panic if n<0. In Golang string is a sequence of bytes. A string literal actually represents a UTF-8 sequence of bytes. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. All other characters are between 1 to 4 bytes. Due to this it is not possible to index a character in a string.  In GO, rune data type represents a Unicode point.  Once a string is converted to an array of rune then it is possible to index a character in that array of rune.

You can learn more the above issue here – https://vikasboss.github.io/number-characters-string-golang/

For this reason in the below program for shuffling a given string , we are first converting a string into a rune array so that we can index into the rune array and use that index to swap characters in that string to shuffle the string.

Code:

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().Unix())

    in := "abcdedf£"

    inRune := []rune(in)
    rand.Shuffle(len(inRune), func(i, j int) {
        inRune[i], inRune[j] = inRune[j], inRune[i]
    })
    fmt.Println(string(inRune))

    rand.Shuffle(len(inRune), func(i, j int) {
        inRune[i], inRune[j] = inRune[j], inRune[i]
    })
    fmt.Println(string(inRune))
}

Output:

It will produce a different output on your machine.

dd£cebaf
feb£cadd

The post Shuffle a string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/shuffle-string-golang/feed/ 0 1921
Pick a random character in string in Go (Golang) https://vikasboss.github.io/pick-random-character-string-golang/ https://vikasboss.github.io/pick-random-character-string-golang/#respond Tue, 31 Mar 2020 15:59:17 +0000 https://vikasboss.github.io/?p=1918 Overview ‘mat/rand’ package of golang contains a Intn function that can be used to generate a pseudo-random number between [0,n). Bracket at the end means that n is exclusive. This function can...

The post Pick a random character in string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

‘mat/rand’ package of golang contains a Intn function that can be used to generate a pseudo-random number between [0,n). Bracket at the end means that n is exclusive. This function can be utilized to pick a random element in a string. We can generate a random between 0 and length-1 of string. Then we can use that random number to index into the string and get the result.

But there is one problem in above approach. In Golang string is a sequence of bytes. A string literal actually represents a UTF-8 sequence of bytes. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. All other characters are between 1 -4 bytes. Due to this it is not possible to index a character in a string.  In GO, rune data type represents a Unicode point.  Once a string is converted to an array of rune then it is possible to index a character in that array of rune.

You can learn more the above issue here – https://vikasboss.github.io/number-characters-string-golang/

For this reason in below program for picking a random in a given string , we  are first converting a string into a rune array so that we can index into the rune array and then return the random character.

Code

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    in := "abcdedf£"
    inRune := []rune(in)
    randomIndex := rand.Intn(len(inRune))
    pick := inRune[randomIndex]
    fmt.Println(string(pick))
}

Output:

One of a,b,c,d,e,f,£

The post Pick a random character in string in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/pick-random-character-string-golang/feed/ 0 1918
Character in Go (Golang) https://vikasboss.github.io/character-in-go/ https://vikasboss.github.io/character-in-go/#respond Mon, 06 Jan 2020 16:52:28 +0000 https://vikasboss.github.io/?p=1107 Overview Golang does not have any data type of ‘char‘. Therefore byte is used to represent the ASCII character. byte is an alias for uint8, hence is of 8 bits or 1...

The post Character in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

Golang does not have any data type of ‘char‘. Therefore

  • byte is used to represent the ASCII character. byte is an alias for uint8, hence is of 8 bits or 1 byte and can represent all ASCII characters from 0 to 255
  • rune is used to represent all UNICODE characters which include every character that exists. rune is an alias for int32 and can represent all UNICODE characters. It is 4 bytes in size.
  • A string of one length can also be used to represent a character implicitly. The size of one character string will depend upon the encoding of that character. For utf-8 encoding, it will be between 1-4 bytes

To declare either a byte or a rune we use single quotes. While declaring byte we have to specify the type,  If we don’t specify the type, then the default type is meant as a rune.

To declare a string, we use double quotes or backquotes. Double quotes string honors escape character while back quotes string is a raw literal string and doesn’t honor any kind of escaping.

Code Example

See the program below. It shows

  • A byte representing the character ‘a
  • A rune representing the pound sign ‘£
  • A string having one character micro sign ‘µ’
package main

import (
    "fmt"
    "reflect"
    "unsafe"
)

func main() {
    //If you don't specify type here
    var b byte = 'a'
    
    fmt.Println("Priting Byte:")
    //Print Size, Type and Character
    fmt.Printf("Size: %d\nType: %s\nCharacter: %c\n", unsafe.Sizeof(b), reflect.TypeOf(b), b)
    
    r := '£'
    
    fmt.Println("\nPriting Rune:")
    //Print Size, Type, CodePoint and Character
    fmt.Printf("Size: %d\nType: %s\nUnicode CodePoint: %U\nCharacter: %c\n", unsafe.Sizeof(r), reflect.TypeOf(r), r, r)

    s := "µ" //Micro sign
    fmt.Println("\nPriting String:")
    fmt.Printf("Size: %d\nType: %s\nCharacter: %s\n", unsafe.Sizeof(s), reflect.TypeOf(s), s)
}

Output:

Priting Byte:
Size: 1
Type: uint8
Character: a

Priting Rune:
Size: 4
Type: int32
Unicode CodePoint: U+00A3
Character: £

Priting String:
Size: 16
Type: string
Character: µ

Caveats

  • Declaring a  byte with a NON-ASCII character will raise a compiler error as below. I tried with a character having a corresponding code as 285
constant 285 overflows byte
  • Only a single character can be declared inside a single quote while initializing byte or a rune. On trying to add two character between single quote, below compiler warning will be generated
invalid character literal (more than one character)

The post Character in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/character-in-go/feed/ 0 1107