How can I check a word is already all uppercase?
You can compare the string with the same string but in uppercase:
'go234' == 'go234'.upcase #=> false
'GO234' == 'GO234'.upcase #=> true
How to convert a string to lower or upper case in Ruby
Ruby has a few methods for changing the case of strings. To convert to lowercase, use downcase
:
"hello James!".downcase #=> "hello james!"
Similarly, upcase
capitalizes every letter and capitalize
capitalizes the first letter of the string but lowercases the rest:
"hello James!".upcase #=> "HELLO JAMES!"
"hello James!".capitalize #=> "Hello james!"
"hello James!".titleize #=> "Hello James!" (Rails/ActiveSupport only)
If you want to modify a string in place, you can add an exclamation point to any of those methods:
string = "hello James!"
string.downcase!
string #=> "hello james!"
Refer to the documentation for String for more information.
How to convert lowercase string to uppercase and vice versa?
As another pointed out, swapcase
already exists, but I suspect it's something you're meant to implement on your own
Here's a functional approach
class String
def is_upper?
self == self.upcase
end
def is_lower?
self == self.downcase
end
def head
self[0]
end
def tail
self[1..-1]
end
def swapcase
if empty?
""
elsif head.is_lower?
head.upcase + tail.swapcase
elsif head.is_upper?
head.downcase + tail.swapcase
else
head + tail.swapcase
end
end
end
puts "abcdE".swapcase
#=> ABCDe
The looping pattern in swapcase
is pretty obnoxious and should be decomposed out into a generic function
class String
def is_upper?
self == self.upcase
end
def is_lower?
self == self.downcase
end
def head
self[0]
end
def tail
self[1..-1]
end
def map &f
if empty?
""
else
yield(head) + tail.map(&f)
end
end
def swapcase
map do |x|
if x.is_lower?
x.upcase
elsif x.is_upper?
x.downcase
else
x
end
end
end
end
It works the same but swapcase
is a lot nicer now
puts "abcdE".swapcase
#=> ABCDe
How to check if a string is all upper or lower case in Go?
You can of course compare the upper and lower cased strings in their entirety, or you can short-circuit the comparisons on the first failure, which would be more efficient when comparing long strings.
func IsUpper(s string) bool {
for _, r := range s {
if !unicode.IsUpper(r) && unicode.IsLetter(r) {
return false
}
}
return true
}
func IsLower(s string) bool {
for _, r := range s {
if !unicode.IsLower(r) && unicode.IsLetter(r) {
return false
}
}
return true
}
Is there an existing library method that checks if a String is all upper case or lower case in Java?
Guava's CharMatchers tend to offer very expressive and efficient solutions to this kind of problem.
CharMatcher.javaUpperCase().matchesAllOf("AAA"); // true
CharMatcher.javaUpperCase().matchesAllOf("A SENTENCE"); // false
CharMatcher.javaUpperCase().or(CharMatcher.whitespace()).matchesAllOf("A SENTENCE"); // true
CharMatcher.javaUpperCase().or(CharMatcher.javaLetter().negate()).matchesAllOf("A SENTENCE"); // true
CharMatcher.javaLowerCase().matchesNoneOf("A SENTENCE"); // true
A static import for com.google.common.base.CharMatcher.*
can help make these more succinct.
javaLowerCase().matchesNoneOf("A SENTENCE"); // true
In Ruby, can I check if a string contains a letter without using regular expressions?
Try this
str.count("a-zA-Z") > 0
The count
function accepts character sets as argument.
This might still fail with ArgumentError: invalid byte sequence in UTF-8
though. If your input is invalid there is likely no way around fixing the encoding.
NB, this scans the entire string, but so does downcase
. For a performance benchmark see Eric's answer, performance varies a lot between worst case and best case scenario. As always though, readability comes before premature optimization.
How to check if there are 2 upper case letters, 3 lower case letters, and 1 number in JAVA
This looks like a homework question so I won't solve it directly. However here are some steps you could take:
Create a method to validate the input (
public static boolean isValid(String str)
)Convert the
String
to a characterArray
. (There's a method for that!)Iterate over the letters and keep track of how many upper and lower case letters there are and how many digits there are. (Using
Character.isDigit()
,Character.isUpperCase()
andCharacter.isLowerCase()
)If all the requirements are met, return true. Return false otherwise.
Related Topics
Rails - Multi Tenant Application with Customization Framework
Having Difficulty Accessing Validation Errors in Sinatra
Capybara Trouble Filling in Js Modal
Why Does 'Puts(Nil or 4)' Fail in Ruby
Cron Is Running in Home Directory Instead of File Directory
Shortening Socket Timeout Using Timeout::Timeout(N) Does Not Seem to Work for Me
Advice on How to Validate Names and Surnames Using Regex
How to Replace the Characters in a String
Why Is the << Operation on an Array in Ruby Not Atomic
The Program 'Rails' Is Currently Not Installed
Ruby Is Already Using the Class Name of My Model
Rails Cancan and State MAChine - Authorizing States
Differencebetween Unicorn and Unicorn_Rails
+= Operator Appears to Modify Frozen String
Error Installing "Kgio-2.9.2" Gem on Windows
Error:'Incompatible Library Version' SQLite3-1.3.11 in Rails