Strings
import std/[strutils, strformat, algorithm]
import std/unicode except strip, split, splitWhitespace, toUpper, toLower, capitalize, reversed, align, alignLeft, repeat
let s = "Hallo Nim-Welt"
echo s.len, " ", s.toUpperAscii, " ", s.toLowerAscii
echo s[0], " ", s[0..4], " ", s[^4..^1]
echo s.find("Nim"), " ", s.contains("Welt"), " ", s.startsWith("Hallo"), " ", s.endsWith("x")
echo s.replace("Welt", "Erde")
echo s.split(' ')
echo "a,b,,c".split(',')
echo @["x", "y", "z"].join("-")
echo " trim ".strip & "|"
echo "ab".repeat(3), " ", "7".align(4, '0'), " ", "x".alignLeft(3) & "|"
echo "42".parseInt + 1, " ", "3.5".parseFloat * 2, " ", "ff".parseHexInt
echo s.count('l'), " ", s.toUpperAscii.count('L')
echo "Nim".len, " ", "ä".len, " ", "äöü".runeLen
echo capitalizeAscii("wort"), " ", s.reversed
echo "x=" & $42 & " y=" & $3.5
echo "a b c".splitWhitespace
echo "CamelCase".normalize
echo "Zeile1\nZeile2".splitLines
echo "$1 und $2, $1 nochmal" % ["a", "b"]
echo isDigit('7'), " ", isAlphaAscii('x'), " ", isUpperAscii('A')
let name = "Mia"
let alter = 17
echo &"{name} ist {alter} Jahre alt, nächstes Jahr {alter + 1}"
echo fmt"{3.14159:.2f} | {42:5} | {42:<5}| {255:x} | {255:b} | {1234567:>10}"
echo fmt"{name:>6}|{name:<6}|{name:^6}|"14 HALLO NIM-WELT hallo nim-welt H Hallo Welt 6 true true false Hallo Nim-Erde @["Hallo", "Nim-Welt"] @["a", "b", "", "c"] x-y-z trim| ababab 0007 x | 43 7.0 255 3 3 3 2 3 Wort @['t', 'l', 'e', 'W', '-', 'm', 'i', 'N', ' ', 'o', 'l', 'l', 'a', 'H'] x=42 y=3.5 @["a", "b", "c"] camelcase @["Zeile1", "Zeile2"] a und b, a nochmal true true true Mia ist 17 Jahre alt, nächstes Jahr 18 3.14 | 42 | 42 | ff | 11111111 | 1234567 Mia|Mia | Mia |
Dateien
import std/[os, strutils, algorithm]
let pfad = getTempDir() / "nim-demo.txt"
writeFile(pfad, "eins\nzwei\ndrei\n")
echo readFile(pfad).splitLines.len
let f = open(pfad, fmAppend)
f.writeLine("vier")
f.close()
for zeile in lines(pfad):
echo "> ", zeile
var n = 0
for zeile in lines(pfad):
inc n
echo n
echo fileExists(pfad), " ", getFileSize(pfad)
let (verz, name, endung) = splitFile(pfad)
echo name, endung
removeFile(pfad)
echo fileExists(pfad)
echo "a" / "b" / "c.txt"
echo "/tmp/x/y.txt".extractFilename, " ", "/tmp/x/y.txt".parentDir
echo existsEnv("PATH"), " ", getEnv("NICHT_GESETZT_XYZ", "Standard")
echo paramCount()
let dir = getTempDir() / "nimdir"
createDir(dir)
writeFile(dir / "a.txt", "A")
writeFile(dir / "b.txt", "B")
var namen: seq[string]
for art, p in walkDir(dir):
namen.add(extractFilename(p))
namen.sort()
echo namen
removeDir(dir)4 > eins > zwei > drei > vier 4 true 20 nim-demo.txt false a/b/c.txt y.txt /tmp/x true Standard 0 @["a.txt", "b.txt"]
JSON
import std/json
let daten = parseJson("""{"name": "Mia", "alter": 17, "hobbys": ["Lesen", "Rad"], "adresse": {"stadt": "Berlin"}}""")
echo daten["name"].getStr
echo daten["alter"].getInt + 1
echo daten["hobbys"][1].getStr
echo daten["adresse"]["stadt"]
echo daten["hobbys"].len
echo daten.hasKey("alter"), " ", daten.hasKey("x")
for h in daten["hobbys"]:
echo "Hobby: ", h.getStr
let neu = %*{"name": "Tom", "punkte": [1, 2, 3], "aktiv": true}
echo neu
echo neu.pretty
type Punkt = object
x, y: int
let p = parseJson("""{"x": 3, "y": 4}""").to(Punkt)
echo p.x + p.y
echo %pMia
18
Rad
"Berlin"
2
true false
Hobby: Lesen
Hobby: Rad
{"name":"Tom","punkte":[1,2,3],"aktiv":true}
{
"name": "Tom",
"punkte": [
1,
2,
3
],
"aktiv": true
}
7
{"x":3,"y":4}Merke
strutilsundstrformatliefern Textfunktionen undfmt"..."/&"..."- Dateien:
readFile,writeFile,lines,open; Pfade mit/und dem Modulos std/json:parseJson,%*,.to(Typ)konvertieren JSON und Objektestrutils.%ersetzt$1,$2in einer Vorlage;fmtformatiert Zahlen wie printf
Aufgabe
Lies eine Textdatei und gib die längste Zeile aus.