I have a question how can I count the number of punctuation symbols in a string.
Here is my code snippet
fun punctuationCount(stringValue: Editable): Int {
val trimmedStr = stringValue.trim()
return if (trimmedStr.isEmpty()) {
0
} else {
return trimmedStr.split("[a-zA-Z&&[ \t]]".toRegex()).size
}
}
CodePudding user response:
You can make use of the built-in count
function on CharSequence
to count the occurrence of arbitrary characters.
val testString = "Some string containing punctuation, like !, @, #, etc."
val punctuationChars = setOf('!', '@', '#', '$', '%', '.')
val occurrences = testString.count { char -> char in punctuationChars }
println("I've counted $occurrences occurrences!")
If you need this functionality more often, you might want to extract it into an extension function, e.g.
fun CharSequence.countCharacters(searchedCharacters: Set<Char>): Int =
this.count { char -> char in searchedCharacters }
val punctuationChars = setOf('!', '@', '#', '$', '%', '.')
fun CharSequence.countPunctuationCharacters(): Int =
this.countCharacters(punctuationChars)
Then you can do
val occurrences = testString.countPunctuationCharacters()
Thanks to @CryptoFool for the example string.
CodePudding user response:
Here's a version of your function that demonstrates what you're asking for:
fun punctuationCount(stringValue: String): Long {
val trimmedStr = stringValue.trim()
return if (trimmedStr.isEmpty()) {
return 0
} else {
val exp = Pattern.compile("\\p{Punct}")
val matcher = exp.matcher(trimmedStr)
return matcher.results().count()
}
}
If you don't like the Pattern library's definition of what constitutes punctuation, you can replace the line that computes the Pattern object with something like this that lets you define punctuation characters explicitly:
val exp = Pattern.compile("[!@#\$%.]")
CodePudding user response:
Another variation - the Unicode standard defines a bunch of categories (see 4.4) that a character can fall into, and those are present in Kotlin, so you could make use of those if you like
import kotlin.text.CharCategory.*
val punctuation = setOf(
DASH_PUNCTUATION, START_PUNCTUATION, END_PUNCTUATION,
CONNECTOR_PUNCTUATION, OTHER_PUNCTUATION
)
val testString = "Some string containing punctuation, like !, @, #, etc."
fun main() {
testString.count { it.category in punctuation }.run(::println)
}
>>> 8
You'd have to decide if those cover your needs, or which ones to include and exclude (and symbols are a different category class). Just throwing it out there as an option / something to know about!