json parse error with double quotes

Javascript unescapes its strings and json unescapes them as well. the first string ( ‘{“result”: [“lunch”, “\”Show\””] }’ ) is seen by the json parser as {“result”: [“lunch”, “”Show””] }, because \” in javascript means “, but doesn’t exit the double quoted string. The second string ‘{“result”: [“lunch”, “\\\”Show\\\””] }’ gets first unescaped to {“result”: … Read more

How can I parse a String to BigDecimal? [duplicate]

Try this // Create a DecimalFormat that fits your requirements DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setGroupingSeparator(‘,’); symbols.setDecimalSeparator(‘.’); String pattern = “#,##0.0#”; DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols); decimalFormat.setParseBigDecimal(true); // parse the string BigDecimal bigDecimal = (BigDecimal) decimalFormat.parse(“10,692,467,440,017.120”); System.out.println(bigDecimal); If you are building an application with I18N support you should use DecimalFormatSymbols(Locale) Also keep in mind … Read more

Does C# have a String Tokenizer like Java’s?

You could use String.Split method. class ExampleClass { public ExampleClass() { string exampleString = “there is a cat”; // Split string on spaces. This will separate all the words in a string string[] words = exampleString.Split(‘ ‘); foreach (string word in words) { Console.WriteLine(word); // there // is // a // cat } } } … Read more

How can I remove extra whitespace from strings when parsing a csv file in Pandas?

You could use converters: import pandas as pd def strip(text): try: return text.strip() except AttributeError: return text def make_int(text): return int(text.strip(‘” ‘)) table = pd.read_table(“data.csv”, sep=r’,’, names=[“Year”, “Make”, “Model”, “Description”], converters = {‘Description’ : strip, ‘Model’ : strip, ‘Make’ : strip, ‘Year’ : make_int}) print(table) yields Year Make Model Description 0 1997 Ford E350 None … Read more

std::lexical_cast – is there such a thing?

Only partially. C++11 <string> has std::to_string for the built-in types: [n3290: 21.5/7]: string to_string(int val); string to_string(unsigned val); string to_string(long val); string to_string(unsigned long val); string to_string(long long val); string to_string(unsigned long long val); string to_string(float val); string to_string(double val); string to_string(long double val); Returns: Each function returns a string object holding the character representation … Read more

Is there any generic Parse() function that will convert a string to any type using parse?

System.Convert.ChangeType As per your example, you could do: int i = (int)Convert.ChangeType(“123”, typeof(int)); DateTime dt = (DateTime)Convert.ChangeType(“2009/12/12″, typeof(DateTime)); To satisfy your “generic return type” requirement, you could write your own extension method: public static T ChangeType<T>(this object obj) { return (T)Convert.ChangeType(obj, typeof(T)); } This will allow you to do: int i = “123”.ChangeType<int>();

Why can’t this SimpleDateFormat parse this date string?

First, three-char months are to be represented by MMM. Second, one-two digit hours are to be represented by h. Third, Mar seems to be English, you’ll need to supply a Locale.ENGLISH, else it won’t work properly in machines with a different default locale. The following works: SimpleDateFormat sdf = new SimpleDateFormat(“MMM dd yyyy h:mm:ss:SSSa”, Locale.ENGLISH); … Read more

JSONArray cannot be converted to JSONObject error

Change JSONObject data = jsonObj.getJSONObject(“data”); to JSONArray data = jsonObj.getJSONArray(“data”); As value of data is JsonArray not JSONObject. And to Get individual Ids and Field Names, you should loop through this JSONArray, as follows: for(int i=0; i<data.length(); i++) { JSONObject obj=data.getJSONObject(i); String id = obj.getString(“Id”); String value = obj.getString(“FieldName”); Log.d(“Item name: “, value); }