string-parsing

Long.getLong() failing, returning null to valid string

狂风中的少年 提交于 2019-11-27 17:08:28
问题 I've spent the past two hours debugging what seems extremely unlikely. I've stripped the method of a secondary Android Activity to exactly this: public void onClick(View v) { String str = "25"; long my_long = Long.getLong(str); } // onClick (v) And yeah, I get a crash with the good ol' NullPointerException: 09-11 02:02:50.444: ERROR/AndroidRuntime(1588): Uncaught handler: thread main exiting due to uncaught exception 09-11 02:02:50.464: ERROR/AndroidRuntime(1588): java.lang

In JavaScript / jQuery what is the best way to convert a number with a comma into an integer?

风格不统一 提交于 2019-11-27 15:42:51
问题 I want to convert the string "15,678" into a value 15678. Methods parseInt() and parseFloat() are both returning 15 for "15,678." Is there an easy way to do this? 回答1: The simplest option is to remove all commas: parseInt(str.replace(/,/g, ''), 10) 回答2: One way is to remove all the commas with: strnum = strnum.replace(/\,/g, ''); and then pass that to parseInt: var num = parseInt(strnum.replace(/\,/g, ''), 10); But you need to be careful here. The use of commas as thousands separators is a

Get number of characters read by sscanf?

天涯浪子 提交于 2019-11-27 13:55:19
I'm parsing a string (a char* ) and I'm using sscanf to parse numbers from the string into doubles, like so: // char* expression; double value = 0; sscanf(expression, "%lf", &value); This works great, but I would then like to continue parsing the string through conventional means. I need to know how many characters have been parsed by sscanf so that I may resume my manual parsing from the new offset. Obviously, the easiest way would be to somehow calculate the number of characters that sscanf parses, but if there's no simple way to do that, I am open to alternative double parsing options.

Code to parse user agent string?

戏子无情 提交于 2019-11-27 08:50:14
As strange as I find this, I have not been able to find a good PHP function anywhere which will do an intelligent parse of a user agent string? Googled it for about 20 minutes now. I have the string already, I just need something that will chop it up and give me at least browser/ver/os. Know of a good snippet anywhere? symcbean The get_browser() function has been available in PHP for quite a long a time. The PHP manual is free, can be downloaded in various formats and viewed online (with comments) Martin Vseticka https://github.com/browscap/browscap-php - this is a standalone library that aims

preg_match array items in string?

大憨熊 提交于 2019-11-27 07:24:44
问题 Lets say I have an array of bad words: $badwords = array("one", "two", "three"); And random string: $string = "some variable text"; How to create this cycle: if (one or more items from the $badwords array is found in $string) echo "sorry bad word found"; else echo "string contains no bad words"; Example: if $string = "one fine day" or "one fine day two of us did something" , user should see sorry bad word found message. If $string = "fine day" , user should see string contains no bad words

Splitting on spaces, except between certain characters

我的梦境 提交于 2019-11-27 06:32:27
问题 I am parsing a file that has lines such as type("book") title("golden apples") pages(10-35 70 200-234) comments("good read") And I want to split this into separate fields. In my example, there are four fields: type, title, pages, and comments. The desired result after splitting is ['type("book")', 'title("golden apples")', 'pages(10-35 70 200-234)', 'comments("good read")] It is evident that a simple string split won't work, because it will just split at every space. I want to split on spaces

How to separate full name string into firstname and lastname string?

自闭症网瘾萝莉.ら 提交于 2019-11-27 00:59:25
问题 I need some help with this, I have a fullname string and what I need to do is separate and use this fullname string as firstname and lastname separately. 回答1: This will work if you are sure you have a first name and a last name. string fullName = "Adrian Rules"; var names = fullName.Split(' '); string firstName = names[0]; string lastName = names[1]; Make sure you check for the length of names . names.Length == 0 //will not happen, even for empty string names.Length == 1 //only first name

Parse time of format hh:mm:ss [closed]

北慕城南 提交于 2019-11-26 23:18:36
问题 How can I parse a time of format hh:mm:ss , inputted as a string to obtain only the integer values (ignoring the colons) in java? 回答1: As per Basil Bourque's comment, this is the updated answer for this question, taking into account the new API of Java 8: String myDateString = "13:24:40"; LocalTime localTime = LocalTime.parse(myDateString, DateTimeFormatter.ofPattern("HH:mm:ss")); int hour = localTime.get(ChronoField.CLOCK_HOUR_OF_DAY); int minute = localTime.get(ChronoField.MINUTE_OF_HOUR);

Convert String with Dot or Comma as decimal separator to number in JavaScript

那年仲夏 提交于 2019-11-26 20:51:55
问题 An input element contains numbers a where comma or dot is used as decimal separator and space may be used to group thousands like this: '1,2' '110 000,23' '100 1.23' How would one convert them to a float number in the browser using JavaScript? jQuery and jQuery UI are used. Number(string) returns NaN and parseFloat() stops on first space or comma. 回答1: Do a replace first: parseFloat(str.replace(',','.').replace(' ','')) 回答2: The perfect solution accounting.js is a tiny JavaScript library for

Safe integer parsing in Ruby

笑着哭i 提交于 2019-11-26 18:53:33
问题 I have a string, say '123' , and I want to convert it to 123 . I know you can simply do some_string.to_i , but that converts 'lolipops' to 0 , which is not the effect I have in mind. I want it to blow up in my face when I try to convert something invalid, with a nice and painful Exception . Otherwise, I can't distinguish between a valid 0 and something that just isn't a number at all. EDIT: I was looking for the standard way of doing it, without regex trickery. 回答1: Ruby has this