Golang Strings

28/12/2020
Chưa phân loại

Strings package in Golang

Overview

In this lesson on String package in Golang, we will study various examples on managing Strings and see how Strings package helps us in regards to Strings Handling in Go programming language. We will get started now.

Starting with Go

Just to make sure we are on the right track of running Go programs, here is the directory structure which I made for my Hello World program:

package main

import "fmt"

func main() {
    fmt.Printf("Hello, world.n")
}

We can run the above program with following command:

go run hello.go

Once we run this command, here is the output you will see:

Now that looks good. Let’s move to our main agenda.

String Handling

Let’s start our journey of handling Strings in Go. We will be exploring the strings package in Go to study various examples. Let’s get started.

 Comparing Strings

Comparing Strings is the most common operation done in the any programming language actually. Let’s look at some code snippet now:

package main

import (
"fmt"
"strings"
)

func main() {
    var str1 string = "Shubham"
    var str2 string = "Linux"
    var str3 string = "linux"
    var str4 string = "Linux"

    fmt.Println(strings.Compare(str1, str2))
    fmt.Println(strings.Compare(str2, str3))
    fmt.Println(strings.Compare(str4, str2))
}

Again, we can run the above program with following command:

go run StringCompare.go

Once we run this command, here is the output you will see:

So, here is the output explained:

  • 1 appears as the first String comes after the second String alphabetically
  • -1 comes as ‘Linux’ comes before ‘linux’ when ASCII values are compared
  • comes when String objects are found to be exactly the same

Containing Strings

Now, we will check if a String is a substring of another String. Let’s look at some code snippet now:

package main

import (
"fmt"
"strings"
)

func main() {
    var mainString string = "Linux"
    fmt.Println(strings.Contains(mainString, "ux"))
    fmt.Println(strings.Contains(mainString, "UX"))
    fmt.Println(strings.Contains(mainString, "Lin"))
}

Again, we can run the above program with following command:

go run StringContains.go

Once we run this command, here is the output you will see:

In this case, output is self-explanatory.

Index of Substring

We can also find the index where the given substring is found first. Let’s look at some code snippet now:

package main

import (
"fmt"
"strings"
)

func main() {
    var mainString string = "Shubham"
    var subs string = "bha"

    fmt.Println(strings.Index(mainString, subs))
}

We can run the above program with following command:

go run StringIndex.go

Once we run this command, here is the output you will see:

Replacing part of Strings

We can replace part of Strings in Go as well. Let’s look at some code snippet now:

package main

import "fmt"
import s "strings"

func main() {
    var mainString string = "Linux"
    var toReplace string = "Li"
    var newPart string = "Te"

    fmt.Println(s.Replace(mainString, toReplace, newPart, 1))
}

In above program, we also saw a slightly different way of importing a package and assigning it an alias name. We can run the above program with following command:

go run StringReplace.go

Once we run this command, here is the output you will see:

Splitting Strings

We can even break Strings in Go by providing a common pattern which might be occurring. Here is an example:

package main

import "fmt"
import s "strings"

func main() {
    var mainString string = "L-i-n-u-x"
    var splitter string = "-"
    fmt.Println(s.Split(mainString, splitter))
}

We can run the above program with following command:

go run StringSplit.go

Once we run this command, here is the output you will see:

Conclusion

To study, Strings package in Go is very deep and it is never possible to get enough. Read more examples for the package and try as much as possible on your own.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Driftnet on Debian: Sniffing images within a network

In this tutorial we’ll sniff a network to intercept graphical content from the devices’ traffic.We’ll learn how to...
29/12/2020

Install Latest Oracle Java JDK/JRE 7/8/9 on Linux

Oracle Java is the key foundation for every type of networked application developed. It is the global standard for developing...
28/12/2020

Installing JDK on Debian 10

Java Development Kit or JDK in short is used to develop and run Java applications. There are two variants of JDK, a) OpenJDK...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024