Convert string to integer type in Go?
For example strconv.Atoi. Code: package main import ( “fmt” “strconv” ) func main() { s := “123” // string to int i, err := strconv.Atoi(s) if err != nil { // … handle error panic(err) } fmt.Println(s, i) }
For example strconv.Atoi. Code: package main import ( “fmt” “strconv” ) func main() { s := “123” // string to int i, err := strconv.Atoi(s) if err != nil { // … handle error panic(err) } fmt.Println(s, i) }
Warning: This does not consider newlines. For a more in-depth answer, see this SO-question instead. (Thanks, Ed Morton & Niklas Peter) Note that escaping everything is a bad idea. Sed needs many characters to be escaped to get their special meaning. For example, if you escape a digit in the replacement string, it will turn … Read more
echo “$string” | tr xyz _ would replace each occurrence of x, y, or z with _, giving A__BC___DEF__LMN in your example. echo “$string” | sed -r ‘s/[xyz]+/_/g’ would replace repeating occurrences of x, y, or z with a single _, giving A_BC_DEF_LMN in your example.
This should give the location of the files that contain your pattern: Get-ChildItem -Recurse | Select-String “dummy” -List | Select Path
You can use the change command to replace the entire line, and the -i flag to make the changes in-place. For example, using GNU sed: sed -i ‘/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.’ /tmp/foo
grep -Fxq “$FILENAME” my_list.txt The exit status is 0 (true) if the name was found, 1 (false) if not, so: if grep -Fxq “$FILENAME” my_list.txt then # code if found else # code if not found fi Explanation Here are the relevant sections of the man page for grep: grep [options] PATTERN [FILE…] -F, –fixed-strings … Read more
Both styles are used within the Go’s standard libraries. if len(s) > 0 { … } can be found in the strconv package: http://golang.org/src/pkg/strconv/atoi.go if s != “” { … } can be found in the encoding/json package: http://golang.org/src/pkg/encoding/json/encode.go Both are idiomatic and are clear enough. It is more a matter of personal taste and … Read more
Here’s how you remove all the whitespace from the beginning and end of a String. (Example tested with Swift 2.0.) let myString = ” \t\t Let’s trim all the whitespace \n \t \n ” let trimmedString = myString.stringByTrimmingCharactersInSet( NSCharacterSet.whitespaceAndNewlineCharacterSet() ) // Returns “Let’s trim all the whitespace” (Example tested with Swift 3+.) let myString = … Read more
Paul’s solution provides a simple, general solution. The question asks for the “the fastest and simplest way”. Let’s address the fastest part too. We’ll arrive at our final, fastest code in an iterative manner. Benchmarking each iteration can be found at the end of the answer. All the solutions and the benchmarking code can be … Read more
When you concatenate strings, you need to allocate memory to store the result. The easiest to start with is String and &str: fn main() { let mut owned_string: String = “hello “.to_owned(); let borrowed_string: &str = “world”; owned_string.push_str(borrowed_string); println!(“{}”, owned_string); } Here, we have an owned string that we can mutate. This is efficient as … Read more