Golang String, Golang StringBuilder, Strings Library

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

STROING

👍︎︎ 1 👤︎︎ u/torrso 📅︎︎ Mar 23 2021 🗫︎ replies
Captions
today we are talking about strings string builders and the strings of built-in library in go these are the basic building blocks of any software project so don't go anywhere what's up ninjas welcome back to the golang dojo this channel is all about the gopro human language and becoming golang ninjas together for the next few dozen videos i'll be going through some of the most popular technical books under the sun related to golang so if you want to learn the go programming language but you hate reading technical books this is the channel for you because i'll be doing all that work for you and produce a video content and in the current series i'm going through the book the go program language and here's what i've learned so far in chapter three welcome aboard to the hello world program open on the golan which is an idea by hijab's brain so if we actually run the commander to run the program we should be able to see that it is printing out hello world which is what is written here sometimes we take this a simple instruction for granted and i forget that this is just a string of it itself so if i actually try to declare it separately this is how you declare a variable a string variable in go and then you just try to print it out so if you haven't already make sure to check out my other video where i talked about how to declare variables and types if this it doesn't look familiar to you so if we actually try to run it again we should be able to see that it is putting out the exact same thing there's quite a few built-in functionalities in go on string that we can utilize such as the length of function that we can call wrapping around the string that we just declared so if we actually try to run this program it should give us the length of this string here five characters for hello and five characters applies that the empty space would be 11 characters and the length of the string if we want to access the individual characters of the string we can absolutely do that by providing the index of which string that we actually want to read and we're just going to provide 0 which is going to show us the capital h here so if we want to run the program again it is going to show us the ascii value of this said character and if we actually want to print out the character itself and then we would need to use the printf function and then present c and provide the same value and we should be able to see that it is printing out h here if we want to access a certain portion of the string we can absolutely do that by let's just put a new line first by calling the string using this if we want to you uh capture the first five characters and that we need just need to provide the starting index and the in the index and then if we try to run the program again we should be able to see that it is printing out hello here we can accomplish the same even if we get rid of the starting index because by default it is going to start from at zero similarly we can also provide it a starting index instead of a ending index so what this will do is it will show us at the string starting from index 6 to the very end so if we try to run this we should be able to see world along with that we can concatenate strings by using the plus operator like such and this is the exact same thing as plus equals again and if we try to print this out we should be able to see hello world again again and it is indeed the case you can also use your usual string literals in a go as well if we have a hello world string here um copy it a couple of times we can put maybe a new line here a tab a backspace so if we try to print this out we should be able to see that it is indeed having a new line after hello here and then we have a tab between the two words and then now here we have a backspace because we actually have a space here but it's gone because we have a backspace little here so strings are slice of a byte so they are actually interchangeable uh let's say that we have a abc that contains a string of abc and then we have a byte string or byte slice and we are declaring it as a slice of bytes and then we are providing it a string so we've actually tried to print these out abc and then b we should be able to see that is putting out abc twice and that is indeed the case except the second time it is putting out its ascii values but that is okay so if we actually try to print it out slightly differently we should be able to see uh abc twice as you can see it is the case here and strings and bytes slices are interchangeable so if we actually have a let's say call it abc 2 and then we are declaring it as a string but then we pass in the byte here we don't really need to do anything extra for it to recognize that hey this is a contains the exact same content as if this is a string so if we actually try to print this out we should be able to and also let's add a new line here we should be able to see that this will just give us abc and indeed strings and bite slices are interchangeable if you aren't that familiar with slices that is a topic that i haven't talked about yet on this channel which i will address in the future so make sure to subscribe to golan dojo for a future video dedicated it's a slices but the brief description in the first slice is it is a data structure in a go that makes this it is kind of similar to a waitlist in java that makes your life a little bit easier when you're developing gold software go also has a native support of ntfa encoding for non-ascii characters if you have been subscribed for a little while you should probably have seen me try to print out hello world in a chinese before so if we actually try to type in chinese of a world or hello world you should be able to see it perfectly fine in chinese and if we actually try to print out the first a character though of this world strain it is going to print give us some ascii value that does not really make sense um that is because this one if i actually try to print out the length of this string here it is actually not going to be two bytes it is actually six as bite so if we actually instead of putting out the first character if we try to print out the first three then we will be able to see the first half of the world in a chinese here so that's about what i want to cover on strings so let's next talk about the strings library so the string library if we just try to print out the strings library here we have a list of different functions that we can utilize some of the more common ones would be to uppercase maybe which will turn the entire string into uppercase strings as you can see it is printing out hello world all in capitalized cases and then if we can do the same if we um called to lowercase then we would see all of the characters into lowercases other common ones let me try i think um maybe has a prefix so does it contain hello as it's a prefix so this one should give us a true value and something like has specifics does it contain hello as its suffix this one should give us a false because at the suffix it is going to be world instead of hello um and then if we actually try to want to print this have a return true then we just need to have it um have the surface provided the actual surface so that this one will return true here and then some other common ones will be like replace so you would provide the actual string that you want to perform replacement on and then you want to provide the old word that you want to replace and the new word that you want to replace it with maybe you want to print out hello twice here and we want to replace it exactly once or maybe if you have a multiple uh world then you can replace it multiple times so if we have maybe you have a two worlds here then if you actually replace one it will only take one of them and replace it with hello so let's see what this one will look like and then do one that will replace both of the worlds here so if i actually print it out we're going to start from the bottom and as you can see it is getting it is replacing both of the worlds here and this one is only replacing the first appearance of world and then it's not going to do anything for the second appearance of world and this one is returning true returning false and returning true all perfectly fine and then we have the uppercase and the lowercase as the first two instructions here as you can see the strings library is definitely a very handy tool that you can utilize to make your job a lot easier so that you don't have to write the exact same code over and over again to do some simple operations like replacing all of your characters into uppercase to lowercase to have prefix and cervix and replacing the different words within a string and so on all right so let's now talk about string builders so what is a string builder a string builder is a tool that you can utilize it to build your strings go figure instead of knowing the value knowing the string value that you want to assign it to a variable instead as you are getting more information as you have the computer go through the different instructions then you can build gradually on that string builder instead of knowing exactly what value to assign it to a string variable so we can get started by declaring a variable called as b and that will be of a type string builder and if we actually try to just print out there's a string builder wide away we find now that string we find out that the string has absolutely nothing it's just going to be empty so i can maybe add something here this is a string builder like that and run it again and as you can see that string builder is does not really contain any sort of information and now we can start building up our string so we can do that by saying we want to write it to write a string into this string builder maybe we want to write hello and if we were to print out this again then we should be able to see that it is printing out hello because we've written a string into this string builder we can also get a similar information by printing out its capacity cap and its length so this one would actually give us at the whole capacity maybe that the entire string does not occupy the uh all the space that the string builder has allocated then the length that you are getting would not will be smaller than the capacity so let's see what they are as you can see in the very beginning uh the capacity is going to be zero because there's no need for the string builder to allocate any space yet and then we have a length which is also going to be zero because it is a empty string that lives in the string builder thus far and it will actually copy and paste the exact same instructions and we should be able to see a increase a cap cap as well as length with its length usually smaller well definitely smaller or equal to the capacity as you can see it is a zero and zero and then we have eight and five and the reason that we have eight and five is that we typically have zero and then we have two and then four and then eight and then keep on doubling and as we find out that we don't have enough space for the string builder or we can increase the capacity manually by doing a calling the growth function maybe we want to roll it by a hundred so if we try to print these again we should be able to see something different here all right so now we see a 1 is 16 and 5. 5 doesn't change because uh the length of the the string doesn't change it still only contains a hello which contains five characters five bytes but why do we actually get a 116 instead of one of eight because it was uh it was a five it was eight here and then we are adding a hundred and why do we get 116. the reason for that is if we actually go see the implementation of this equal function and if we actually click on this one you can see that is it it is actually doubling the current cap and then uh plus whatever is passed in so if the initial um a cab is eight when that we double that which will turn into 16 and then we plus 100 and that equals to 116. and of course that that doesn't really change the content of the string itself if we try to print it out again it's still going to give us just hello it only grows at the cap it doesn't really do anything to the content as you probably already figured out because the length is still the same however if you regret the decision to grow the string or you regret the decision that you've actually written something into the string builder you can definitely call the reset function and at which point all of your if we copy and paste these lines these will be 0 and 0 and an empty string again and remember what we said about strings in the very beginning we said that strings are just slices of a bite so we can definitely try to write to um the strings by passing in slices of bites or by slicers size slices excuse me so we can definitely just do a bite and then have 65 65 and 65 and this will just give us aaa and there we go we have aaa here another very common string operation is the conversion between strings and maybe other data types like numbers and since we talked about integer in our last video actually to check out that video we just tried to demonstrate the conversion between strings and integers so we can go ahead and declare a variable of integers one two three and now we are trying to convert this integer value into a string value so what we do is i call the built-in function in a 2 array it's into string but whatever we can just put in the integer as the parameter and then we can print out y here and that this should give us the one two three if you are not convinced that this is actually returning a string we can also print out the type using printf here so if we print out the type of y this one should give us a string along with that we can definitely do the conversion at the other way around instead of converting an integer to a string we can convert a string back to an integer so in order to do that we will declare a variable z as a placeholder for the conversion from a string to integer and we will call the same library again except this time we will call the string to integer or a way to integer and we pass in the string as a parameter and then we mistakes may say colon and then print out z right here and as you can see we are indeed getting uh one two three and then we if we actually print out the type of a z we should be able to see that it is indeed an integer variable now one thing that you might have noticed is that i've written a line of code that is a pretty odd because it had a pretty odd declaration of a variable when i'm trying to declare the integer z that is because if we actually try to hold the control key and click on this function here the return type actually contains two things the very first one is the actual integer that we are looking for and the second thing is actually an error and if we go back to our function uh to our instruction that we've written by writing the underscore instead of some variable holder i'm actually indicating that i don't care about the area that is being returned now this may or may not be the best practice but it's sufficient enough for demonstration purposes but what we do care about or what's actually important is that this is actually a unique way of handling errors in go and this is definitely a full-blown topic that deserves a separated video of itself so if you want to learn more about error handling in go and how it differs the way of how it handles how it differs from other languages then make sure to subscribe to this channel so that you won't miss out on that future video now a reminder that for the next few dozens of videos i'll be going over some of the most popular technical books related to the golang so if you want to learn the go pokemon language and you hate winning technical books then this is the channel for you so make sure to subscribe to the channel and let's become a goal and name just together with that let's shout again in our next [Music] video you
Info
Channel: Golang Dojo
Views: 2,139
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, golang in 2021, golang tutorial, go tutorial, golang for beginners, golang strings, golang stringbuilder, golang string library, golang slice, golang strings tutorial, strings in golang, golang string builder, golang string, go strings, go string builder, go stringbuffer, go stringbuilder, string builder in go, stringbuilder in go, stringbuilder in golang, string builder in golang, string builders in golang, string in go, strings in go
Id: l-BgjOr5FJY
Channel Id: undefined
Length: 22min 43sec (1363 seconds)
Published: Sat Mar 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.