Split bash string by newline characters

Another way: x=$’Some\nstring’ readarray -t y <<<“$x” Or, if you don’t have bash 4, the bash 3.2 equivalent: IFS=$’\n’ read -rd ” -a y <<<“$x” You can also do it the way you were initially trying to use: y=(${x//$’\n’/ }) This, however, will not function correctly if your string already contains spaces, such as ‘line … Read more

How to index a String in Rust

Yes, indexing into a string is not available in Rust. The reason for this is that Rust strings are encoded in UTF-8 internally, so the concept of indexing itself would be ambiguous, and people would misuse it: byte indexing is fast, but almost always incorrect (when your text contains non-ASCII symbols, byte indexing may leave … Read more

require file as string

If it’s for a (few) specific extension(s), you can add your own require.extensions handler: var fs = require(‘fs’); require.extensions[‘.txt’] = function (module, filename) { module.exports = fs.readFileSync(filename, ‘utf8’); }; var words = require(“./words.txt”); console.log(typeof words); // string Otherwise, you can mix fs.readFile with require.resolve: var fs = require(‘fs’); function readModuleFile(path, callback) { try { var … Read more

How to get the number of characters in a string

You can try RuneCountInString from the utf8 package. returns the number of runes in p that, as illustrated in this script: the length of “World” might be 6 (when written in Chinese: “世界”), but the rune count of “世界” is 2: package main import “fmt” import “unicode/utf8” func main() { fmt.Println(“Hello, 世界”, len(“世界”), utf8.RuneCountInString(“世界”)) } … Read more

How do I parse a string into a number with Dart?

You can parse a string into an integer with int.parse(). For example: var myInt = int.parse(‘12345’); assert(myInt is int); print(myInt); // 12345 Note that int.parse() accepts 0x prefixed strings. Otherwise the input is treated as base-10. You can parse a string into a double with double.parse(). For example: var myDouble = double.parse(‘123.45’); assert(myDouble is double); … Read more

Swift – Split string over multiple lines

Swift 4 includes support for multi-line string literals. In addition to newlines they can also contain unescaped quotes. var text = “”” This is some text over multiple lines “”” Older versions of Swift don’t allow you to have a single literal over multiple lines but you can add literals together over multiple lines: var … Read more

How can I use grep to find a word inside a folder?

grep -nr ‘yourString*’ . The dot at the end searches the current directory. Meaning for each parameter: -n Show relative line number in the file ‘yourString*’ String for search, followed by a wildcard character -r Recursively search subdirectories listed . Directory for search (current directory) grep -nr ‘MobileAppSer*’ . (Would find MobileAppServlet.java or MobileAppServlet.class or … Read more