Compare variable to text disregarding caps [duplicate] - lua

Here is the thing. I am trying to convert a string in lowercase in Lua, but it's not working. I have done this
String = String:lower()
but it doesn't like it. I am sure that is the way to do it, I've seen it done before. A few sites suggest it might be a problem caused by a wrong version of the interpreter.
Any ideas?

You're right, this is one of the ways to do it. It would only not work and throw errors if your "String" variable is not a string.
Personally, i usually prefer to use something like..
myString = string.lower(myString)
But its really the same as doing
myString = myString:lower()
assuming that myString is actually a string, however.
The "long" version has one advantage, it actually works if myString is a number, while the second one errors in that case.

Related

Using sortedArrayUsingSelector: with non-letter characters at the end?

Hopefully this isn't a repetitive question, but I haven't been able to find the solution anywhere. I'm sorting keys in a NSDictionary using
sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)
Everything is working great, except this returns my "#" key first. I'd prefer to return it after the "A-Z" keys. It's the only non-alphabet key.
Any help is greatly appreciated. Thanks!
So use sortedArrayUsingComparator instead, and use a custom comparator block that first checks for your "#" key, and specifies it should be after other strings, and returns the result of localizedCaseInsensitiveCompare for everything else. See if you can work that out, and if not post back with the code you tried and I'll help you debug it. (Are you using Objective-C or Swift?)

Escape bad characters for iOS UILabel?

I'm feeding some NSString data (forum posts queried from my website) into a UILabel as part of my first app. Thing is, sometimes, depending on the content of the post, the Label goes entirely blank. I've tinkered enough to discern that there are certain characters that cause the problem, but I can't quite pin down the full set.
Is there a collected list of character types to watch out for with this kind of thing? And even better, is there a method for escaping them, or automatically converting them into something more acceptable?
Thank you for helping out a n00b!
Looks like you have whitespace or new line in your string:
Try this:
NSString* labelText = [stringFromWebsite stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
I am trying to give you some clue even you haven't put piece of code here in your question.
You should firstly try to encode that coming String by using appropriate method like encoding:NSUTF8StringEncoding and still and try to set decoded string to your UIlabel.
Another alternative is that you can create some regex for filtering purpose of that coming string. you can find many of similar thread over the google.

passing variable to a string in a Rails controller

I need to pass a variable into a string like this.
HTTParty.get(https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false)
This works fine as a string, but I cant seem to get the syntax correct to make the parameters variables. Can someone please help me with the syntax.
It's not clear what issue you're having (or if the missing quotes are just a typo). Normal string interpolation should do what you want:
bar = "123.45"
plugh = "xyz"
HTTPart.get("blahblah?foo=#{bar}&baz=#{plugh}")
You'll want to url-encode the values as well, most likely, unless they're under control.

Find all URL's in a string in rails 3

I'm building an app in Rails 3 and I need a method to extract all urls from a string and store them in a hash or something. I know I need to use regular expressions but I don't know where exactly to begin with them.
Also, I know about auto_link, but it doesn't quite do what I'm trying to achieve. I just simply need a hash of all the url's from a string.
Thanks!
From http://www.regular-expressions.info/ruby.html
"To collect all regex matches in a string into an array, pass the regexp object to the string's scan() method, e.g.: myarray = mystring.scan(/regex/)."
So you probably need strings that start with "http". So check the docs for that :)
I don't program in Ruby and I'm not very good with regex but maybe this will help you out:
http://www.ozzu.com/programming-forum/url-regex-t104809.html

Regular expression not working when put in an object

I'm trying to store regexes in a database but they're not working when used in a .sub(), even though the same regex works when used directly in .sub() as a string.
regex = Class.object.field // Class.object is an active record containing "\w*\s\/\s"
mystring = "first / second"
mystring.sub(/#{regex}/, '')
// => nil
mystring.sub(/\w*\s\/\s/, '')
// => second
Any insight appreciated!
Thanks,
Matt.
Editing to correct class/object terminology (thanks) & correcting my 2nd example as I had shown #{} wrapped around the working regex (cut & paste SNAFU).
To answer your question: It is not quite what kind of thing your Class.object is. If it's an ActiveRecord, it won't work.
Edit: You obviously found that the problem is Rails escaping the regexp.
An ActiveRecord cannot "contain" your regular expression directly; the regexp will be in one of the fields of your record. In which case you'd want to do something like regexp = Class.object.field_containing_the_regexp.
Even if that is not the case, I suspect that the problem is that your regexp is something other than a string. You can quickly test this by using
puts "My regexp: #{regexp}"
The string that you will see in the output will be the one that is used for the regexp.
A String is not a Regexp. You have to create a Regexp object first.
regex = Regexp.new("\w*\s\/\s")
Turns out my regexp didn't cater for all cases - \w didn't account for symbols. After checking in rails console, and seeing the screwey escaping I was alreasdy half-way down the wrong track.
Thanks for the help.

Resources