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
Related
/[\s\W]#(\w+)/g
This is a very simple that will still correctly capture most desired tweeter hashtag cases, EXCEPT for the special case where the hashtag is actually the first word with no leading characters:
http://regexr.com/3h2rr
If we make the first character set lazy it will correctly capture the first hashtag, but also incorrectly not reject hashtags with leading alphanumeric characters
http://regexr.com/3h2ru
A programatic workaround can simply be: "always adding a space to the begining of the tweet string", thus bypassing the limitation of this simple expression, but now I'm really curious to see how to do this the right way.
Cheers
/[\s\W]*#(\w+)/g
This should allow for no preceding character to be needed, might not be the most beautiful way to do this though.
I've been looking around, and I am wondering whether there is a simple way to capitalize all words in a UITextField, while leaving certain words (such as of, the, or, etc.) lowercase, unless they are the first word of the phrase.
This is an
Example of the Effect I'm Trying to Convey.
One of the methods I've found is to search the text field value for the certain words and replace them with lowercase versions, as the user types a new word or character, perhaps listening for the space bar.
I'm not sure if the method above is best practice, or whether my searches haven't been broad enough to find a solution already in the mix.
I was originally thinking something along these "pseudocode" lines:
When value of textfield is changed
Get current value textfield
For each word in value:
If the word matches ("For", "Of", "The", etc.) and the word is not the first word in the value:
Change the word to lowercase, and replace word
Go to next word
My actual question is mainly one of performance. Would this method be overly strenuous on my application? If so, are there any better solutions?
Thank you all for your assistance!
Update:
Thanks to holex, cluemein, and others who have already commented and answered. I will try your solutions when I get the opportunity to do so.
A better way then converting the words to lowercase is to capitalize the words that are NOT those words you specified. Set up if statements to capitalize the beginning letter of the first word, and to capitalize the words following that if they are not the words you specified. Then, if you want to make sure the specified words weren't capitalized after the first word, use an else statement. "pseudocode" example:
Capitalize letter of first word;
Move on to next word;
While not end of textfield (or while typing):
if word is not ("the"|"and"|"of"|"or"|...):
capitalize first letter;
else:
set first letter to lowercase;
move to next word at space;
This will on average be roughly twice as fast as going back through the text looking for the specified words in terms of runtime. This isn't the code you would use, but the algorithm you would implement. Also, take into account what holex said about spaces. I leave how you implement this algorithm up to you. Just to clarify, this algorithm is for both autocapitalizng and auto-setting to lower case.
I'm trying to use CFStringTokenizer with kCFStringTokenizerUnitSentence to split a string into sentences. The first problem I'm having is that sentences need to be capitalized in order for them to be recognized as sentences. If not, it just thinks it's part of the previous sentence.
I'm splitting user-entered text so I'm expecting the text to be very unclean.
Is there something else I can do with CFStringTokenizer to have it detect uncapitalized sentences? Or will I have to use another method of splitting altogether?
I followed the answer on this SO question for my implementation:
How to get an array of sentences using CFStringTokenizer?
NOTE: After testing a bit more it seems that with kCFStringTokenizerUnitSentence, if a '!' or a '?' is followed by an uncapitalized sentence, it will recognize the sentence. Also, if one of those punctuation marks is followed by a sentence without a space between the '!' and the first word, it will still separate.
So the one case I need to work around is a '.' followed by an uncapitalized sentence.
ANOTHER OPTION I found, if you're getting the text from a textField, is to use this:
textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
It will automatically capitalize sentences so you don't have to worry about converting for CFStringTokenizer. It still doesn't account for edge cases like abbreviations, but at least in my case the user will have an option to delete the auto-capitalization if it's wrong.
You can convert the input string to all uppercase first and then run it through CFStringTokenizer and use the ranges to get the substrings of the original input string. But you must be careful here because some characters might become more than 1 character after conversion to uppercase.
I am trying to capture a conditional of years in RegEx. Basically, if they implement just a two digit year, I want to make it a four digit year. So if they put :
1/2/08
I want to make it :
1/2/2008
Any ideas?
One [pretty nasty] way using regex:
"1/2/08".sub! /\/(\d{2})$/, '/20\1'
Wouldn't it be better to just parse the string into a date object, though? Then you can treat it as a date properly! :)
You could split on '/' and if the last component has a length of two you prepend 20 and then assemble the date again.
You could split the string up using
(.*/)(..)$
and then substitute with something like
$120$2
to put the string back together (tested with http://www.regexplanet.com/simple/).
You might need to think about what you want to happen for dates in the 20th century - this approach will recognise 21/01/98 as 21/01/2098 which might not be what you want... It might be better to parse the string out properly rather than just blindly regex it!
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