Different between parseInt() and valueOf() in java?

Well, the API for Integer.valueOf(String) does indeed say that the String is interpreted exactly as if it were given to Integer.parseInt(String). However, valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int. If you want to enjoy the potential caching benefits of Integer.valueOf(int), you could also use this eyesore: Integer k = Integer.valueOf(Integer.parseInt(“123”)) … Read more

How can I check if a string represents an int, without using try/except?

with positive integers you could use .isdigit: >>> ’16’.isdigit() True it doesn’t work with negative integers though. suppose you could try the following: >>> s=”-17″ >>> s.startswith(‘-‘) and s[1:].isdigit() True it won’t work with ‘16.0’ format, which is similar to int casting in this sense. edit: def check_int(s): if s[0] in (‘-‘, ‘+’): return s[1:].isdigit() … Read more

How can I convert a std::string to int?

In C++11 there are some nice new convert functions from std::string to a number type. So instead of atoi( str.c_str() ) you can use std::stoi( str ) where str is your number as std::string. There are version for all flavours of numbers: long stol(string), float stof(string), double stod(string),… see http://en.cppreference.com/w/cpp/string/basic_string/stol

How to concatenate a std::string and an int

In alphabetical order: std::string name = “John”; int age = 21; std::string result; // 1. with Boost result = name + boost::lexical_cast<std::string>(age); // 2. with C++11 result = name + std::to_string(age); // 3. with FastFormat.Format fastformat::fmt(result, “{0}{1}”, name, age); // 4. with FastFormat.Write fastformat::write(result, name, age); // 5. with the {fmt} library result = fmt::format(“{}{}”, … Read more

Designing function f(f(n)) == -n

You didn’t say what kind of language they expected… Here’s a static solution (Haskell). It’s basically messing with the 2 most significant bits: f :: Int -> Int f x | (testBit x 30 /= testBit x 31) = negate $ complementBit x 30 | otherwise = complementBit x 30 It’s much easier in a … Read more

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