Creating a sting of particular size?

We can create a byte array ([5]byte) of fixed size.
Is there any way to create a fixed size for the string?
I know that string Max size is 2^31 - 1 and this will be the size if we create a variable of string datatype.

But I just wanted to know can we create a string of 20 bytes or 30 bytes.

Thanks.

Hi. What do you want to use it for? It’s maybe easier to answer the question then.

Suppose if have a string of 1000 characters and user wants to get only 100 characters of string at first call.
He will create a string of 100 bytes(suppose if 1 char occupies 1 byte). Then I have to find out the size of the string(which user has created) and the pass only that many characters.

Here 100 is just an example user can create a string of size as they want.

Hope this example will make some sense.

Hi Akhil,

First, please look at how strings are defined in Go:

https://golang.org/ref/spec#String_types

So by definition, strings have fixed sizes.

The way your example is usually handled in Go is to use a slice. If you need a string type, you can easily convert a slice of bytes into a string.

Thanks @jayts

Using slices we can do it as I mentioned in my first post itself. But I was looking to do using string. So from your link, we cannot do it with strings as they are fixed size.

You can also check strings.Builder

You don’t have to use a slice. Try this:

https://play.golang.org/p/CTflaHmIw5i

Hi.

I’ts more complex then this. Strings in go is by default encoded in utf8 so if you just take the first 100 bytes can you end up with an illegal string if your string contains anything but ascii (7 bit) characters. Utf-8 can represent single character with 1 up to 4 bytes (see https://en.wikipedia.org/wiki/UTF-8).

2 Likes

See here https://play.golang.org/p/e1hNDsJQ33K

Just taking the first two bytes of the string “Räksmörgås” (shrimp sandwich in Swedish) results in an illegal string because the letter ä (a umlaut) takes two bytes. But if you convert the string to runes and pick the first 2 and convert them back to a string it will be “Rä”

2 Likes

Hi
This is different from my question. Here you creating a string with value. But I declare a empty string and then get the size of that.
example:

var a string
//Here the  string size is 2^31-1
// I am trying to change the size of the declared string so that its declaration size will be 10bytes or 20 bytes

Hope this will give you enough info on my post.

Hi @johandalabacka

From your example, I see that the user has to pass the number of characters(n) he wants.
So the user will get a string of n characters.

Thanks

1 Like

You need to limit the size on your own using runtime checks, there is no way to restrict a strings length at compile time.

1 Like

Thanks

I had a small bug in my example. If the string is shorter than the wanted character it could panic with a slice index out of bounds. Corrected:

https://play.golang.org/p/9S9DLQfrZ46

1 Like

To make it faster use a StringBuilder and just fill it with the first n runes

func Left(s string, n int) string {
	sb := strings.Builder{}
	for i, r := range s {
		if i >= n {
			break
		}
		sb.WriteRune(r)
	}
	return sb.String()
}
1 Like

Maybe same for Romanian language. We have diacritics.

For example, my name contains Ionuț

Maybe you can use this

3 Likes

Exactly all letters which isn’t in the American alfabet A-Z and a-z will require at least 2 bytes (up to 4)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.