Form Construction and Usage
Form Construction and Usage of the Function get
Creates a Form class and uses get to obtain the value mapped to the key.
Code:
In this example, the function get of the Form class is used to obtain the value 2 of the specified key = 1.
import encoding.url.*
main(): Int64 {
var s = Form("1=2&2=3&1=2&&")
print(s.get("1").getOrThrow())
return 0
}
Running result:
2
Form Construction and Usage of the Function get in the Case of Duplicate Keys
Creates a Form class and uses get to obtain the value mapped to the key.
Code:
In this example, the function get of the Form class is used to obtain the first value %6AD of the specified key = 1. %6A in the value is decoded to** j**. Therefore, the value jD is obtained.
import encoding.url.*
main(): Int64 {
var s = Form("2=3&1=%6AD&1=2")
// Decodes **%6A** into **j**. For duplicate keys, get is called to obtain the first value **jD**.
print(s.get("1").getOrThrow())
return 0
}
Running result:
jD
Form Construction and Usage of Other Functions
Calls add, set, and clone to print the changes before and after the output.
Code:
import encoding.url.*
main(): Int64 {
var f = Form()
// Adds values **v1** and **v2** to the key **k**.
f.add("k", "v1")
f.add("k", "v2")
// When the get method is called, the first value is obtained.
println(f.get("k").getOrThrow())
// Sets the value of the key **k** to **v**.
f.set("k", "v")
println(f.get("k").getOrThrow())
let clone_f = f.clone()
// Adds a key-value pair to the cloned clone_f.
clone_f.add("k1", "v1")
// Uses get to obtain the value **v1**.
println(clone_f.get("k1").getOrThrow())
// The original f does not have the key **k1**. Therefore, the value is the default value **kkk**.
println(f.get("k1") ?? "kkk")
0
}
Running result:
v1
v
v1
kkk