Substring/Top and tail: Difference between revisions

Go solution
(Ada version added)
(Go solution)
Line 127:
puts(1, strip_last("write")) -- strip last character
puts(1, strip_both("brooms")) -- strip both first and last characters</lang>
 
=={{header|Go}}==
Go strings are byte arrays that can hold whatever you want them to hold. Common contents are ASCII and UTF-8. You use different techniques depending on how you are interpreting the string. The utf8 package functions shown here allows efficient extraction of first and last runes without decoding the entire string.
<lang go>package main
 
import (
"fmt"
"utf8"
)
 
func main() {
// ASCII contents: Interpreting "characters" as bytes.
s := "ASCII"
fmt.Println("String: ", s)
fmt.Println("First byte removed: ", s[1:])
fmt.Println("Last byte removed: ", s[:len(s)-1])
fmt.Println("First and last removed:", s[1:len(s)-1])
// UTF-8 contents: "Characters" as runes (unicode code points)
u := "Δημοτική"
fmt.Println("String: ", u)
_, sizeFirst := utf8.DecodeRuneInString(u)
fmt.Println("First rune removed: ", u[sizeFirst:])
_, sizeLast := utf8.DecodeLastRuneInString(u)
fmt.Println("Last rune removed: ", u[:len(u)-sizeLast])
fmt.Println("First and last removed:", u[sizeFirst:len(u)-sizeLast])
}</lang>
Output:
<pre>
String: ASCII
First byte removed: SCII
Last byte removed: ASCI
First and last removed: SCI
String: Δημοτική
First rune removed: ημοτική
Last rune removed: Δημοτικ
First and last removed: ημοτικ
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
1,707

edits