s1 := "testestestest"
s2 := s1[3:7]
s2 contains "test"
- Evan
Sorry, I should have mentioned that. bflm's solution will work for that case.
- Evan
func main() {
str1 := "тестестестест"
str2 := string([]int(str1)[3:7])
fmt.Println(str2)
}
Sorry.
'rune' is just a short name for a unicode code-point. (almost a
character, but some characters are made up of multiple code-points)
In Go it's just an int.
>> But such mixing of UTF-8 encoding with codepoint access by index is
>> often probably a wrong way. Either work only with []rune (for random
>> access), or only with UTF-8 (for sequential access). In the later case
>> one can also range the UTF-8 to get the runes on by one.
--
=====================
http://jessta.id.au
The "strings" package doesn't expect any kind of encoding for the
data. It just deals with strings as immutable []byte.
The "utf8" package doesn't deal with actual characters either, it just
deals with converting utf8 encoded data in to unicode code-points.
The "unicode" package is where this kind of functionality should go
because working out what is actually a 'character' in unicode is more
work than a simple slice.
> On Jun 30, 7:41 am, bflm <befelemepesev...@gmail.com> wrote:
>> Oops, I wrote wrong:
>>
>> func main() {
>> str1 := "тестестестест"
>> str2 := string([]int(str1)[3:7])
>> fmt.Println(str2)
>>
>> }
>>
>> Sorry.
--
=====================
http://jessta.id.au
str1 := "тестестестест"
str2 := str1[3:7]
... is that 3 and 7 are magic numbers. In a real slicing, those
numbers have to come from somewhere sensible, since We
All Know that strings are byte-sequences not character-sequences.
And once the indexes have been found in a rune-respecting
way then the slice just works.
Chris
--
Chris "allusive" Dollin
-rob
I probably should have said 'glyph' instead of 'character'.
I was referring to the possibility of the use of combining characters
(http://en.wikipedia.org/wiki/Combining_character).
Where a 'glyph' entered by a user or displayed on a screen may be
multiple code-points. Thus slicing on code points without an
understanding of what those code-points mean might miss code-points
that the user expects to be there.
- jessta
--
=====================
http://jessta.id.au