Golang path and filepath Examples.
Using filepath.Base
The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base:
package main
import (
"fmt"
"path/filepath"
)
func main() {
path ...
An example program using numbers.
Integers And Floating Point Numbers
Go has several different types to represent numbers. Generally we split numbers into two different kinds: integers and floating-point numbers.
Make a main.go file containing the ...
In Python, there are 2 ways to extract all the numbers contained in a string.
Using List Comprehension
List comprehensions provide a concise way to create lists.
If you only want to extract only positive integers, try the following:
#!/usr/bin/...
In Python, using the str.split method is the easiest way to split a string into a list
Using split Method
The str.split(sep=None, maxsplit=- 1) method returns a list of the words in the string, using sep as the delimiter string. If maxsplit is given...
In Python, there are 2 ways to capitalize the first letter of each word in a string.
Using title Method
The str.title() method returns a titlecased version of the string where words start with an uppercase character and the remaining characters are...
In Python, there are 3 ways to remove all empty strings from a list of strings.
Using filter Function
The filter(function, iterable) function constructs an iterator from those elements of iterable for which function returns true.
The following exam...
In Python, there are 3 ways to remove all whitespace in a string.
Using str.replace Function
The str.replace(x) function returns a copy of the string with all occurrences of substring old replaced by new.
See the following example:
#!/usr/bin/p...
In Python, there are 2 ways to get the filename without the extension from a path.
Using pathlib Module
You can use .stem from pathlib to get the filename without the extension from a path, it really simple. For example,
#!/usr/bin/python3
# Im...
In Golang, there are 2 ways to index characters in a string.
Using individual characters
In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. Strings behave like slices of bytes. A rune is an integer value i...
In Python, there are 2 ways to extract extension from filename.
Using os.path.splitext Method
The os.path.splitext(path) method splits the pathname path into a pair (root, ext) such that root + ext == path, and the extension, ext, is empty or begins...
In Python, there are 4 ways to remove a trailing newline.
Using rstrip Method
The str.rstrip() method returns a copy of the string with trailing characters removed. For example,
#!/usr/bin/python3
s = "\nab c\r\n"
# str.rstrip([chars])
n = s....
In Rust, there are 3 ways to split a string.
Using split Function
An iterator over substrings of this string slice, separated by characters matched by a pattern. See the following example:
fn main() {
let s = "abc1bcd2e";
let _split = s.sp...
In Golang, there are 2 ways to read a file line-by-line.
Using bufio.Scanner Function
Scanner provides a convenient interface for reading data such as a file of newline-delimited lines of text. The ones that are currently implemented are:
packa...
In Python, there are 3 ways to split a list into equally sized chunks.
Using List Comprehension
List comprehensions provide a concise way to create lists. The following example:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# initialize
lst = ['a'] *...
Splitting a String into a Slice in Golang.Using strings.Split Function
Use the strings.Split function to split a string into its comma separated values.
package main
import (
"fmt"
"strings"
)
func main() {
s := "Japan,Germany"
// func S...