How can I capitalize the first letter of each word in a string using JavaScript?

I’m trying to write a function that capitalizes the first letter of every word in a string (converting the string to title case). For instance, when the input is “I’m a little tea pot”, I expect “I’m A Little Tea Pot” to be the output. However, the function returns “i’m a little tea pot”. This … Read more

Python: most idiomatic way to convert None to empty string?

What is the most idiomatic way to do the following? def xstr(s): if s is None: return ” else: return s s = xstr(a) + xstr(b) update: I’m incorporating Tryptich’s suggestion to use str(s), which makes this routine work for other types besides strings. I’m awfully impressed by Vinay Sajip’s lambda suggestion, but I want … Read more

What is the fastest substring search algorithm?

OK, so I don’t sound like an idiot I’m going to state the problem/requirements more explicitly: Needle (pattern) and haystack (text to search) are both C-style null-terminated strings. No length information is provided; if needed, it must be computed. Function should return a pointer to the first match, or NULL if no match is found. … Read more

Why is it discouraged to accept a reference to a String (&String), Vec (&Vec), or Box (&Box) as a function argument?

I wrote some Rust code that takes a &String as an argument: fn awesome_greeting(name: &String) { println!(“Wow, you are awesome, {}!”, name); } I’ve also written code that takes in a reference to a Vec or Box: fn total_price(prices: &Vec<i32>) -> i32 { prices.iter().sum() } fn is_even(value: &Box<i32>) -> bool { **value % 2 == … Read more

Is it good practice to use java.lang.String.intern()?

The Javadoc about String.intern() doesn’t give much detail. (In a nutshell: It returns a canonical representation of the string, allowing interned strings to be compared using ==) When would I use this function in favor to String.equals()? Are there side effects not mentioned in the Javadoc, i.e. more or less optimization by the JIT compiler? … Read more