How do you calculate the length of the string that would be returned by Array::pack? Is there something like Python's calcsize?
array.pack("").count I would say. Not really the fastest method, but it works.
By making an interpreter complying to the specifications found in Array::pack.
Or, reusing the existing implementation to count the number of characters instead of appending them to a string.
Related
I'm trying to pass in /\!|\.|\?/ to the separator argument for readlines. It seems it's not possible. Or is it?
f.readlines(/\!|\.|\?/)
I know the alternative is to use read and split, which accepts Regexp, but I want to know if this is also possible with readlines
IO#readlines expects a string, not a regular expression. But the desired behaviour might be easily achieved with read + split since according to the documentation readlines “reads the entire file”:
f.read.split /\!|\.|\?/
Please also read the valuable comment by #tom-lord with a significant improvement suggestion.
UTF-8 string in Julia cannot use slice operator because it slice the byte index of string not character. For example
s = "ポケットモンスター"
s[1:4]
s[1:4] will be "ポケ" not "ポケット".
I would like to know the simplest and most readable for get UTF-8 sub-string in Julia.
Perhaps this question calls attention to some missing functions in the standard string library (which is supposed to undergo changes in the next version of Julia). In the meantime, if we define:
substr(s,i,j) = s[chr2ind(s,i):chr2ind(s,j)]
Then,
substr(s,1,4)
Would be "ポケット"
You might want to consider using UTF32String instead of UTF8String, if you are going to be doing this a lot, and only converting to UTF8String if necessary, when you are finished.
I have a text filed which allows user to input numbers. This is what I did:
[_textfield setText:[NSString stringWithFormat:#"%d", [_textfield.text intValue]]];
Basically, I convert text in text filed to integer, then convert back to string. This will ensure the text is numbers only.
Now I need to store the text in _textfield into core data. I was wondering wether should use string as the attribute type or integer.
I know integer is a more sensible option. But to this case, every time the view is loaded, I need fetch this data and set to the _textfield. If I use integer as the attribute type, I have to convert to string each time. I know what I need to do is simply:
_textfield.text = [numberFromCoreData stringValue];
I don't need to compare, sort or do any arithmetic computation with that number, so should I just use string as the attribute type?
Integer searching is significantly faster than string searching. That is the single most compelling reason to use numbers in your persistence layer. Numbers also can sort differently than strings.
For performance reasons I would never use a string when I know the value is always going to be an integer. Control the input, force it to only accept numbers and keep your data integrity.
It depends how you need to use that field. In almost every case, integer data should be stored as an integer type, but not always. You definitely want an integer type if you'll ever be using that field in a case where its numeric value counts in some way. That includes sorting (because it's a hell of a lot faster with numeric fields), comparing numeric values, or any kind of mathematical operation.
But there's are exceptions. For example, in some cases fields which initially seem to be inherently numeric turn out not to be so. Like a "size" field which is normally an integer. But on closer inspection it turns out that some sizes are specified as "8 - 10", "12 - 14", etc. This happened in one app I worked on a couple of years ago. In that case I ended up using two fields for the data-- a numeric "sortSize" that could be used for sorting, and a string "displaySize" that included the full string.
It's probably not what you want but why don't you use a keyboard type "number Pad" for your textfield?
With that, you would be sure that you have only numbers into your textfield.
Honestly I can't think of a compelling reason. Strings in general take up more storage space than Integers but in the modern world of computing this isn't much of an issue. If you aren't really pushing you processor too hard I'd go with what is convenient.
From the most basic way of thinking about it an integer is a number but for a string the computer needs to know when the string ends, starts, and what is in it so its a little bigger.
I have a long string, consisting of multiple sentences, of various length, divided by a "-".
I want to iterate over the string and extract everything between the -'s, preferably to an array.
From another thread I found something that gets me pretty close, but not all the way:
longString.scan( /-([^-]*)-/)
Needless to say, I am new to Ruby, and especially to RegEx.
What's wrong with using String#split?
longString.split('-')
Why not just use string.split()?
longString.split('-');
I have a field in my Rails model that has max length 255.
I'm importing data into it, and some times the imported data has a length > 255. I'm willing to simply chop it off so that I end up with the largest possible valid string that fits.
I originally tried to do field[0,255] in order to get this, but this will actually chop trailing Unicode right through a character. When I then go to save this into the database, it throws an error telling me I have an invalid character due to the character that's been halved or quartered.
What's the recommended way to chop off Unicode characters to get them to fit in my space, without chopping up individual characters?
Uh. Seems like truncate and friends like to play with chars, but not their little cousins bytes. Here's a quick answer for your problem, but I don't know if there's a more straighforward and elegant question I mean answer
def truncate_bytes(string, size)
count = 0
string.chars.take_while{|c| (a += c.bytes.to_a.length) <= size }.join
end
Give a look at the Chars class of ActiveSupport.
Use the multibyte proxy method (mb_chars) before manipulating the string:
str.mb_chars[0,255]
See http://api.rubyonrails.org/classes/String.html#method-i-mb_chars.
Note that until Rails 2.1 the method was "chars".