I have a camel-cased string, for example: JustAString
.
I would like to form strings of length 4 by following these rules:
Here are the 3 cases that may happen:
ThisIsMyString
will yield TIMS
(capitals);ThisIsOneVeryLongString
will yield TIOV
(first 4 capitals);MyString
will yield MSTR
(capitals + tr
capitalized).I managed to solve the first two cases with this snippet:
str.scan(/[A-Z]/).first(4).join
However, I am not quite sure how can I best modify the above snippet to handle the last case also (or even try something different).
P.S.: The string is guaranteed to have at least a capital and 4 characters. However, if theoretically a capital is missing, the first 4 characters should be taken into consideration. If there aren't 4 characters, the missing characters may be filled in with the first alphabet characters (abcd
). But, as mentioned, these 2 edge cases won't normally happen.
Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter. In a character set a ^ character negates the following characters.
To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase.
To split a string on capital letters, call the split() method with the following regular expression - /(? =[A-Z])/ . The regular expression uses a positive lookahead assertion to split the string on each capital letter and returns an array of the substrings. Copied!
Let's discuss certain ways in which only uppercase letters can be extracted from a string. List comprehension and isupper function can be used to perform this particular task. The list comprehension is primarily used to iterate over the list and isupper function checks for the uppercase characters.
Replace any character(s) that precedes a capital with nothing, then grab the first 4 characters and upcase:
str.gsub(/[^A-Z]+([A-Z])/){$1}[0..3].upcase
This handles no capitals too. As for the edge case of not enough chars, you could append "abcd" but I'd find it cleaner just to do this in a separate line after the fact: output_string = (output_string + "abcd")[0..3] if output_string.length < 4
. This is reads much cleaner and performs (inconsequentially) better if this is really a rare edge case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With