View Category
Output a string to the console
Write the string
"Hello World!" to STDOUT
go
fmt.Printf("Hello, world\n")
fmt.Println("Hello, World!")
Retrieve a string containing ampersands from the variables in a url
My PHP script first does a query to obtain customer info for a form. The form has first name and last name fields among others. The customer has put entries such as
The script variable for first name $_REQUEST
I have tried various functions like urldecode but all to no avail. I even tried encoding the url before the view screen is painted so that the url looks like
Of course this fails for the same reasons. What is a better approach?
"Ron & Jean" in the first name field in the database. Then the edit form script is called with variables such as
"http://myserver.com/custinfo/edit.php?mode=view&fname=Ron & Jean&lname=Smith".
The script variable for first name $_REQUEST
['firstname'] never gets beyond the "Ron" value because of the ampersand in the data.
I have tried various functions like urldecode but all to no avail. I even tried encoding the url before the view screen is painted so that the url looks like
"http://myserver/custinfo/edit.php?mode=view&fname="Ronxxnbsp;xxamp;xxnbsp;Jean"&lname=SMITH". (sorry I had to add the xx to replace the ampersand or it didn't display meaningful url contents the browser sees.)
Of course this fails for the same reasons. What is a better approach?
string-wrap
Wrap the string
Expected output:
> The quick brown fox jumps over the lazy dog. The quick brown fox jumps over t
> he lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox
> jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The qui
> ck brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy
> dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps o
> ver the lazy dog. The quick brown fox jumps over the lazy dog.
"The quick brown fox jumps over the lazy dog. " repeated ten times to a max width of 78 chars, starting each line with "> "
Expected output:
> The quick brown fox jumps over the lazy dog. The quick brown fox jumps over t
> he lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox
> jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The qui
> ck brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy
> dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps o
> ver the lazy dog. The quick brown fox jumps over the lazy dog.
go
package main
import (
"fmt"
"strings"
)
func main() {
str := "The quick brown fox jumps over the lazy dog. "
chs := strings.Split(str, "")
j := 1
for i := 0; i < 10; i++ {
for n, l := 0, len(chs); n < l; n++ {
if j == 1 {
fmt.Print("> ")
}
fmt.Print(chs[n])
if j == 77 {
fmt.Println()
j = 1;
}else {
j++;
}
}
}
}
import (
"fmt"
"strings"
)
func main() {
str := "The quick brown fox jumps over the lazy dog. "
chs := strings.Split(str, "")
j := 1
for i := 0; i < 10; i++ {
for n, l := 0, len(chs); n < l; n++ {
if j == 1 {
fmt.Print("> ")
}
fmt.Print(chs[n])
if j == 77 {
fmt.Println()
j = 1;
}else {
j++;
}
}
}
}
package main
func main() {
chs := []rune("The quick brown fox jumps over the lazy dog. ")
j := 1
for i := 0; i < 10; i++ {
for _, v := range chs {
if j == 1 {
print("> ")
}
print(string(v))
if j == 77 {
println()
j = 1;
}else {
j++;
}
}
}
}
func main() {
chs := []rune("The quick brown fox jumps over the lazy dog. ")
j := 1
for i := 0; i < 10; i++ {
for _, v := range chs {
if j == 1 {
print("> ")
}
print(string(v))
if j == 77 {
println()
j = 1;
}else {
j++;
}
}
}
}
Define a string containing special characters
Define the literal string
"\#{'}${"}/"
go
s := "\\#{'}${\"}/"
s := `"\#{'}${"}/"`
Define a multiline string
Define the string:
"This
Is
A
Multiline
String"
go
text := "This\nIs\nA\nMultiline\nString\n"
text := "This\n" + "Is\n" + "A\n" + "Multiline\n" + "String\n"
var s = `This
Is
A
Multiline
String`
Is
A
Multiline
String`
Define a string containing variables and expressions
Given variables a=3 and b=4 output
"3+4=7"
go
a, b := 3, 4
fmt.Printf("%d+%d=%d\n", a, b, a + b)
fmt.Printf("%d+%d=%d\n", a, b, a + b)
a, b := 3, 4
fmt.Println(a, "+", b, "=", a+b)
fmt.Println(a, "+", b, "=", a+b)
Reverse the characters in a string
Given the string
"reverse me", produce the string "em esrever"
go
package main;
import "utf8";
import "fmt";
func reverse(s string) string {
o := make([]int, utf8.RuneCountInString(s))
i := len(o)
for _, c := range s {
i--
o[i] = c
}
return string(o)
}
func main() {
fmt.Print(reverse("reverse me"));
}
import "utf8";
import "fmt";
func reverse(s string) string {
o := make([]int, utf8.RuneCountInString(s))
i := len(o)
for _, c := range s {
i--
o[i] = c
}
return string(o)
}
func main() {
fmt.Print(reverse("reverse me"));
}
func Reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
Reverse the words in a string
Given the string
"This is a end, my only friend!", produce the string "friend! only my end, the is This"
go
words := strings.Split("This is the end, my only friend!", " ")
nr := len(words)
reversed := make([]string, nr)
for i, word := range words {
reversed[nr - i - 1] = word
}
s := strings.Join(reversed, " ")
fmt.Println(s)
nr := len(words)
reversed := make([]string, nr)
for i, word := range words {
reversed[nr - i - 1] = word
}
s := strings.Join(reversed, " ")
fmt.Println(s)
func reverse(list []string) ([]string) {
if len(list) == 1 {
return list
}
return append(reverse(list[1:]), list[0])
}
func main() {
words := strings.Split("This is the end, my only friend!", " ")
s := strings.Join(reverse(words), " ")
fmt.Println(s)
}
if len(list) == 1 {
return list
}
return append(reverse(list[1:]), list[0])
}
func main() {
words := strings.Split("This is the end, my only friend!", " ")
s := strings.Join(reverse(words), " ")
fmt.Println(s)
}
Text wrapping
Wrap the string
> The quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog.
"The quick brown fox jumps over the lazy dog. " repeated ten times to a max width of 78 chars, starting each line with "> ", yielding this result:
> The quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog.
go
import "fmt"
import "strings"
const WIDTH = 72
func main() {
s := "The quick brown fox jumps over the lazy dog."
words := strings.Split(s, " ")
tmp := words
for i, pos := 0, 0; i < 10; i++ {
for len(words) > 0 {
if pos == 0 {
fmt.Printf("> ")
}
if pos + len(words[0]) > WIDTH {
fmt.Printf("\n")
pos = 0
} else {
fmt.Printf("%s ", words[0])
pos += len(words[0]) + 1
words = words[1:]
}
}
words = tmp
}
fmt.Printf("\n")
}
import "strings"
const WIDTH = 72
func main() {
s := "The quick brown fox jumps over the lazy dog."
words := strings.Split(s, " ")
tmp := words
for i, pos := 0, 0; i < 10; i++ {
for len(words) > 0 {
if pos == 0 {
fmt.Printf("> ")
}
if pos + len(words[0]) > WIDTH {
fmt.Printf("\n")
pos = 0
} else {
fmt.Printf("%s ", words[0])
pos += len(words[0]) + 1
words = words[1:]
}
}
words = tmp
}
fmt.Printf("\n")
}
Remove leading and trailing whitespace from a string
Given the string
" hello " return the string "hello".
go
s := strings.TrimSpace(" hello ")
Simple substitution cipher
Take a string and return the ROT13 and ROT47 (Check Wikipedia) version of the string.
For example:
String is: Hello World #123
ROT13 returns: Uryyb Jbeyq #123
ROT47 returns: w6==@ (@C=5 R`ab
For example:
String is: Hello World #123
ROT13 returns: Uryyb Jbeyq #123
ROT47 returns: w6==@ (@C=5 R`ab
Make a string uppercase
Transform
"Space Monkey" into "SPACE MONKEY"
go
strings.ToUpper("Space Monkey")
Make a string lowercase
Transform
"Caps ARE overRated" into "caps are overrated"
go
strings.ToLower("Caps ARE overRated")
Capitalise the first letter of each word
Transform
"man OF stEEL" into "Man Of Steel"
go
strings.Title(strings.ToLower("man OF stEEL"))
