How do I generate a stream from a string?

public static Stream GenerateStreamFromString(string s) { var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(s); writer.Flush(); stream.Position = 0; return stream; } Don’t forget to use Using: using (var stream = GenerateStreamFromString(“a,b \n c,d”)) { // … Do stuff to stream } About the StreamWriter not being disposed. StreamWriter is just a wrapper … Read more

Are multi-line strings allowed in JSON?

JSON does not allow real line-breaks. You need to replace all the line breaks with \n. eg: “first line second line” can saved with: “first line\nsecond line” Note: for Python, this should be written as: “first line\\nsecond line” where \\ is for escaping the backslash, otherwise python will treat \n as the control character “new … Read more

What does %w(array) mean?

%w(foo bar) is a shortcut for [“foo”, “bar”]. Meaning it’s a notation to write an array of strings separated by spaces instead of commas and without quotes around them. You can find a list of ways of writing literals in zenspider’s quickref.

Extract substring in Bash

You can use Parameter Expansion to do this. If a is constant, the following parameter expansion performs substring extraction: b=${a:12:5} where 12 is the offset (zero-based) and 5 is the length If the underscores around the digits are the only ones in the input, you can strip off the prefix and suffix (respectively) in two … Read more

Remove empty array elements

As you’re dealing with an array of strings, you can simply use array_filter(), which conveniently handles all this for you: print_r(array_filter($linksArray)); Keep in mind that if no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed. So if you need to preserve elements that are i.e. exact … Read more

PHP random string generator

To answer this question specifically, two problems: $randstring is not in scope when you echo it. The characters are not getting concatenated together in the loop. Here’s a code snippet with the corrections: function generateRandomString($length = 10) { $characters=”0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”; $charactersLength = strlen($characters); $randomString = ”; for ($i = 0; $i < $length; $i++) { $randomString … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)