[Solved] Does Java have handy properties such as string.punctuation? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[Solved] Does Java have handy properties such as string.punctuation?

While studying Python I learned about "string" and others modules in standard library. Inside module string there are many handy properties such as accii_letters, digits, etc. I wonder does Java have those handy properties too? I take a look at Javadoc, it lists the method and other things, but I cannot find something similar.

14th Nov 2023, 3:51 AM
Wong Hei Ming
Wong Hei Ming - avatar
7 Answers
+ 3
Javadoc is a great place to look around. But much of the convenience features of Python, are not that handy in Java, and some of it is scattered in other classes. A few places to look: Character (particularly related to your question: isLetter, isDigit, isWhitespace etc. methods) StringReader, StringBuffer Scanner Also each Java class has a toString() method (inherited from Object) that determines the string representation of the object, similar to __repr__ magic method in Python. Another topic for processing strings, is regular expressions, and you will find more info about it if you research the Pattern and Matcher classes. You can check a possible approach to punctuation here: https://www.baeldung.com/java-remove-punctuation-from-string
14th Nov 2023, 6:36 AM
Tibor Santa
Tibor Santa - avatar
+ 4
Zip code validation is actually very easy if you know a little bit of regex. This means exactly what the task says: 5 digits, no other character. s.matches("\\d{5}") https://code.sololearn.com/cLhZPMVWiUHH/?ref=app For the vowel counter you can define a string that contains all the vowels, and check each character. You could use a more sophisticated way also to define a HashSet of the vowels. As an example this method takes advantage of the IntStream and uses a ternary expression to turn each character into a 1 or 0 as counter: s.chars() .map(c -> "aeiouAEIOU".indexOf(c)>=0 ? 1 : 0) .sum(); I particularly love using streams for solving such problems, because we can build nice pipelines from them that represent a sequence of operations and transformations on data. But you can do the same with normal loops as well. https://code.sololearn.com/c3300AUlzMdr/?ref=app
14th Nov 2023, 11:55 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Tibor Santa I just finished "Zip Code Validator" and "Vowel Counter" in code coach hours ago. While coding, I found myself miss the simplicity like <for c in string> in python, end up using lots of <toCharArray()> , <Integer / String.valueOf()> and <contains()> methods to convert the datatype, looping a string and check if character inside a string. Also the "Character" you mentioned, I thought it was Char, similar to integer becomes Integer when I was trying to create a HashMap object yesterday. I asked because string.digits can be handy in "Zip Code Validator" with python, although we can always create a string of digits ourselves. Frankly I have difficulties reading Javadoc and other documentation such as python's. They assume you have good knowledge about computing and throw lots of technical terms to you. In the end I just look at the available methods and then google them again looking for a simple, easy to understand explanation such as geeksforgeeks or w3cschools.
14th Nov 2023, 9:23 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
Wong Hei Ming In Java you can do this: String s = "yourString"; for(char c : s.toCharArray()){ } You can also have a look into StringBuilder which has many useful methods: replace(), insert(), reverse(), setCharAt(), substring() and so on...
14th Nov 2023, 10:48 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
It is the compiler's job to optimize away the unused imports. The main difference between importing specific classes from a package, and everything from a package, is about code readability. If you try using an IDE such as Jetbrains IntelliJ, it will largely handle the imports for you automatically, and if I remember correctly if you import more than two classes from the same package then it will just switch to import * At runtime it does not have any significance, because the bytecode only follows the code path that you are actually using. And more advanced compilers such as GraalVM are able to strip away anything from dependencies and even from the builtins which you never actually used, so the executable is reduced to really tiny size. This is also called tree-shaking and some other languages also have this feature, even some Javascript frameworks. https://stackoverflow.com/questions/8724045/do-unused-import-and-objects-have-a-performance-impact
14th Nov 2023, 1:47 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Denise Roßberg toCharArray() is the method I used. reverse() and substring() seems handy in code coach, definitely research on them soon.
14th Nov 2023, 10:52 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Tibor Santa A slightly side question. The way you import the object / methods with asterisk (*), isn't it not following SOILD you told me in other post? Especially Interface segregation principle or Dependency inversion principle. I know it is far more convenience to import all methods when building software. But loading extra library which will not be unused doesn't mean it consumes more memory and brings potential security breach? It is like some android apps require permissions which are not related to the apps intended function, such as weight scale apps request permission to access camera. The thing I like about python is that it provides all functions to the basic datatype, yet you have to import other library when needed, such as math or re, and you have a choice to import some of the function instead of the whole library.
14th Nov 2023, 1:20 PM
Wong Hei Ming
Wong Hei Ming - avatar