Is there anything out there for a conditional capitalize type function? I want to capitalize a string only if I detect there are a certain percentage of capitals letters. I want to do this because I don't want to run the function on everything, but I do want to run it on strings that I think are written in all caps.
Or I guess is there a way to return how many letters string.downcase does work on?
Thanks!
No, but there is nothing preventing you from putting the call to titleize inside a conditional that checks for the case you're interested in.
Ah ok I think I got it.
string.count('A-Z')/string.length
Related
I have a string eg. String-aa. I want to be able to capitalise every letter after the "-". Is there a straightforward was of achieving this?
I am aware of .capitalize however dont know how to implement for this particular requirement.
You can make use of block form of gsub
"String-aa-bbbb".gsub(/-\w+/){|e| e.upcase}
#=> "String-AA-BBBB"
The above code will capture a letter followed by - and will capitalize it
EZ stuff but after an hour.. =filter(May15!A:S , May15!E:E="Authorization") is yielding a rich populated sheet. However I can't get OR working! Despite it working elsewhere in the sheet. I'd like other possibilities via the same filter. I tried several including the OR this way
=filter(May15!A:S , OR(May15!E:E="Authorization" , May15!E:E="bigwhale", May15!E:E="hi"))
.. to no avail. Any help appreciated. Also, I read somewhere the OR could be accessed using a "+" and that sounded like a neat method.. Thanks!
One possible way is to use RegEx:
=filter(H:H , REGEXMATCH(E:E,JOIN("|",A1:A3)))
put in A1:A3:
Authorization
bigwhale
hi
This trick is useful when you need to add conditions, just paste one more value in cell A4 and use range A1:A4
Another way is to use plus sign:
=FILTER(H:H,(E:E="Authorization")+(E:E="bigwhale")+(E:E="hi"))
I have 2D array in which the second column has domain names of some emails, let us call the array myData[][]. I decided to use ArrayLib in order to search the second column for a specific domain.
ArrayLib.indexOf(myData, 1, domain)
Here is where I found an issue. In myData array, one of the domains look like this "ewmining.com" (pay attention to the w).
While searching for "e.mining.com" (notice the first dot), the indexOf() function actully gave me the row containing "ewmining.com".
This is what is in the array "ewmining.com"
This is what is in the serach string "e.mining.com"
It seams that ArrayLib treats the dot to mean any character. Is this supposed to be the correct behavior? Is there a way to stop this behavior and search for exact match.
I really need help on this issue.
Thanks in advance for your help.
The dot usually represents "any character" in regular expressions. I am not familiar with ArrayLib, but maybe you should look for a way to turn off regular expressions when searching. Otherwise you might have to escape the dot, for example search for e[.]mining[.]com
I have a field -
:Revenue
and it should accept values like 10,000.00, but if I input such value it stores 10 into database instead of 10000.00
What should I do to strip of commas before I save?
I've tried to find a few solutions online but wasn't able to implement them as I found them incomplete. If someone could help me I would really appreciate it.
**The problem now I am facing is that as soon as I enter the value rails converts string in to float value before it can run the gsub function, like if I enter 50,000.00 its converting into float 50.0 before calling the gsub, is there any way to over the to_f method which rails is calling on the string.
Removing commas is pretty simple:
value.gsub(/,/, '').to_f
Keep in mind that European formatting often uses comma as the decimal value separator so your results would be off by a factor of 100 if processing those sorts of numbers.
You can take a String#delete.
"10,000,000.00".delete(',').to_f
# => 10000000.0
I found the solution after looking at few places and combining few solutions, since I had to use gsub before the linking to model has to be done. so I created the method in my controller and called it before create and update action. and wrote the following code in the method
params[:record][:Revenue] = params[:record][:Revenue].gsub(/,/,"")
I'm writing a little scripting language just for a bit of fun and the learning of the codes :P
I would just like your opinions/suggestions. I have an idea but I don't want to include something that people are going to want to spit on. I plan on making this language open source once, soon.
Does anybody think that it would be cool to have something like:
[Foreach] Uppercase Letter s
in Case-Insensitive Word SallySawtheSeafiShandateit:
Count++.
s.Highlight: True.
RunOnce.ProtectedMethod.ActivateProtectedMethod: IsTrue.
[Protected Method.LockTo: [Foreach]].IsTrue
StatusBar.Message: Match for s was found. Total: Count..
RunOnce.ProtectedMethod.Disable.
Explanation: What the above actually does is it searches through a string of text "SallySawtheSeafiShandateit" and highlights every single match. But when it finds the very first match for "s", it runs a method called "IsTrue", and sets the statusbar text to "match was found...". And then deactivates the RunOnce method so it may no longer be accessed, since there's no need for it to be run again.
This might not be the best example, but I think you get the idea. There have been plenty of times where I've needed to do something only once in a foreach loop, but couldn't, without writing a whole bunch of other code.
I figure, atleast this way, everything can be done in just two methods.
Please be brutally honest. :)
Thank you
This just seems like an over-complication of the following structure (in java style):
boolean ranOnce = false;
for (char c : string.toCharArray()) {
if (c != 's') continue;
if (!ranOnce) {
// do stuff once
ranOnce = true;
}
// do other stuff
}
It just seems like extreme over-engineering to me, when a single boolean and an if condition do the trick.
Hm. For this sort of situation I'd normally just use a flag variable and a conditional.
I'd reconsider "runOnce" -- it's a little ambiguous. Does it run the first iteration, the last iteration, somewhere in the middle? From what I can tell it looks like yours runs in the very first iteration, but then again what use would displaying the total count be in the first iteration? You'll know it's just "1".
For my money, I think I'd actually use two keywords that fired events/methods/etc at the first iteration and at the last iteration, respectively.