Use the String.Format() method to replace one or more items in the string with the string representation of a specified object:
String.Format("Hello {0} Foo {1}", "World", "Bar") //Hello World Foo Bar
Given an IObservable<Offer> of offers from merchants to buy or sell some type of item at a fixed price, we can match pairs of buyers and sellers as follows:
var sellers = offers.Where(offer => offer.IsSell).Select(offer => offer.Merchant);
var buyers = offers.Where(offer => offer.Is...
var sequenceOfSequences = new [] { new [] { 1, 2, 3 }, new [] { 4, 5 }, new [] { 6 } };
var sequence = sequenceOfSequences.SelectMany(x => x);
// returns { 1, 2, 3, 4, 5, 6 }
Use SelectMany() if you have, or you are creating a sequence of sequences, but you want the result as one long sequen...
Create the JSONObject using the empty constructor and add fields using the put() method, which is overloaded so that it can be used with different types:
try {
// Create a new instance of a JSONObject
final JSONObject object = new JSONObject();
// With put you can add a name/va...
To check whether a particular String a is being contained in a String b or not, we can use the method String.contains() with the following syntax:
b.contains(a); // Return true if a is contained in b, false otherwise
The String.contains() method can be used to verify if a CharSequence can be fou...
Starting a service is very easy, just call startService with an intent, from within an Activity:
Intent intent = new Intent(this, MyService.class); //substitute MyService with the name of your service
intent.putExtra(Intent.EXTRA_TEXT, "Some text"); //add any extra data to pass to the s...
Server: Start, and wait for incoming connections
//Open a listening "ServerSocket" on port 1234.
ServerSocket serverSocket = new ServerSocket(1234);
while (true) {
// Wait for a client connection.
// Once a client connected, we get a "Socket" object
// that c...
One use of SharedPreferences is to implement a "Settings" screen in your app, where the user can set their preferences / options. Like this:
A PreferenceScreen saves user preferences in SharedPreferences. To create a PreferenceScreen, you need a few things:
An XML file to define the av...
print_r() - Outputting Arrays and Objects for debugging
print_r will output a human readable format of an array or object.
You may have a variable that is an array or object. Trying to output it with an echo will throw the error:
Notice: Array to string conversion. You can instead use the print_r...
Python 2.x2.3
In Python 2.x, to continue a line with print, end the print statement with a comma. It will automatically add a space.
print "Hello,",
print "World!"
# Hello, World!
Python 3.x3.0
In Python 3.x, the print function has an optional end parameter that is what...
A quick way to make a copy of an array (as opposed to assigning a variable with another reference to the original array) is:
arr[:]
Let's examine the syntax. [:] means that start, end, and slice are all omitted. They default to 0, len(arr), and 1, respectively, meaning that subarray that we are ...
You can include another Git repository as a folder within your project, tracked by Git:
$ git submodule add https://github.com/jquery/jquery.git
You should add and commit the new .gitmodules file; this tells Git what submodules should be cloned when git submodule update is run.
A submodule references a specific commit in another repository. To check out the exact state that is referenced for all submodules, run
git submodule update --recursive
Sometimes instead of using the state that is referenced you want to update to your local checkout to the latest state of that s...
A submodule is always checked out at a specific commit SHA1 (the "gitlink", special entry in the index of the parent repo)
But one can request to update that submodule to the latest commit of a branch of the submodule remote repo.
Rather than going in each submodule, doing a git checkout...
This uses the Dropbox Python SDK to create a shared link for a folder:
import dropbox
dbx = dropbox.Dropbox("<ACCESS_TOKEN>")
shared_link_metadata = dbx.sharing_create_shared_link_with_settings("/Testing")
print shared_link_metadata.url
<ACCESS_TOKEN> should be...