String Packages in Go Programming

String Packages In Go Programming

In this article, we’ll learn about String Packages with their functions. The String Packages are a common construct. You see them in every programming language, and strings are made of Unicode runes.

So, Golang has a package called the Unicode Package which provides a set of functions that actually check the properties of the different runes inside the strings. And it’s very useful if you’ve ever done the parsing, you know, you want to parse some string out of whatever file maybe something somebody typed indirectly as user input and you want to check that string when you’re doing parsing you need functions like these. So, there a long set of functions that the Unicode package provides to check the runes Some of them here,

  1. IsDigit(r rune) tells you if the rune is a digit, a numerical digit.
  2. IsSpace(r rune) – is a space character.
  3. IsLetter(r rune) is the letter lowercase
  4. IsPunct(r rune) is punctuation

So, these functions are all binary- boolean types. they return true or false depending on if the rune is what it’s saying it to be if it’s a digit or space or so on.

There are also a set of other functions that perform conversions.

So, some of these conversions ae possible. For instance.

ToUpper(r rune)
ToLower(r rune)

If you can take a lower case(r rune), turn it into an upper case (r rune). So they provide functions for that. So they take an (r rune), like ToUpper, it takes an (r rune), Which is lowercase and returns a which is uppercase and so on. So a Unicode package is useful for that. But there are other packages that are also involved in manipulating strings.

Golang has string packages. So that strings package functions provide are things to directly. It not looking at the individual generally, but look at the whole string.

So, It has string packages. These are common functions that you see in lots of different languages also. So for instance,

1. Compare

Compare(a, b) – It returns an integer comparing two strings. 0 if a==b, -1 if ab,

you give it two strings and it compares to see if they are equal. the compare function, it returns a -1. It returns 0 when they’re equal, it returns a -1 if a is less than b, meaning earlier than it, in alphanumeric order. And the A, it returns a positive one, if A is greater than B, so if A is later than B in alphanumeric order.

2. Contains

Contains(s, substring) – It returns true if the substring is insides, So if you pass S as a string, and then a substring, if that substring is contained inside S, it returns true, otherwise false.

3. HasPrefix

HasPrefix(s, prefix) – It returns true is the string s begins with prefix. so if you pass it a string s and a prefix. If that returns true, if that prefix is the, s starts with a prefix.

4. Index

Index(s, substring) – It returns the index of the first instance of the substring in s. it searches for the substring inside S. And it returns you the index of where the first instance, instance of that substring can be found inside.

String Manipulation

The string packages also provide a set of functions that manipulate and conversion to strings. Now, when we say manipulate, you can say changes string, a string is mutable. But, Golang has a lot of functions that take an existing string and return a new string. That is some modified and some useful way.

1. Replace

Replace(s, old, new, n) – replace returns a copy of the strings with the first n instances of old replaced by new.

Using the replace function, you simply take a string and it allows you to replace instances whenever it finds it in a current of old it replaces it with currents of new. So these are all strings. So there is a big string S Is a substring old and another substring new. Replaces instances of old with instances of new. And return to a new string. So s the original string is not actually changed.

It returns you to a new string with a replacement for ToLower(s), ToUpper(s), so this will take the whole string and change it to lower case or upper case.

2. TrimSpace

TrimSpace(s) is useful. returns a new string with all leading and trailing white space removed. It gets rid of leading and trailing light space from a string.

String Conversion

Another string packages are have a lot of useful functions for strings is the Strconv Package. So generally It provides a set of functions for converting strings from different basic data types, to and from different basic data types.

1. Atoi(s)

It converts a string to an integer if that string represents an integer.

2. FormatFloat

The FormatFloat(f,fmt, prec, bitsize) It basically does a similar thing for float. So it converts a floating-point number into a string representation of that floating-point number.

3. ParseFloat

ParseFloat(s, bitsize) It does the opposite, converts a string to a floating-point number. So if you have a string. 123.45, and in a string it can convert it into a floating-point the number you can actually do the method.

Leave a Reply

Your email address will not be published. Required fields are marked *