How to lowecase only first word in the sentence? [duplicate] - swift

This question already has answers here:
Swift apply .uppercaseString to only the first letter of a string
(31 answers)
Closed last year.
I have a text "Hello Word" "Word Hello",
How can i get "hello Word" "word Hello" (for example)
'''
let string1 = "Hello Word"
let referenceString1 = "hello Word"
let string2 = "Word Hello"
let referenceString2 = "word Hello"
'''

Get first letter of the first word and make it lowercase , then remove first letter and add the rest.
extension StringProtocol {
var lowerCaseFirts: String { prefix(1).lowercased() + dropFirst() }
}
let str = "Hello World"
print(str.lowerCaseFirts)

def fun(sentence : str) -> str:
words = sentence.split()
if not (words[0])[0].islower():
words[0] = (words[0])[0].lower() + (words[0])[1:]
out = " ".join(words)
return out
if __name__ == '__main__':
sentence1 = "Hello Everyone"
out1 = fun(sentence1)
print(sentence1, " : ", out1)
sentence2 = "HOW ARE YOU"
out2 = fun(sentence2)
print(sentence2, " : ", out2)
Output:
Hello Everyone : hello Everyone
HOW ARE YOU : hOW ARE YOU

The selected answer works perfectly for the OPs use case:
Hello Word
However, if we are speaking about first word in a sentence, there could be a more complex string with multiple sentences:
Hello Word. JELLO world? WEllo Word.\n ZEllo world! MeLLow lord.
In such a case, using REGEX might also work to solve both the scenarios.
Someone with more savvy REGEX skills might be able to improve the REGEX
extension String
{
func withLowerCaseSentences() -> String
{
do
{
// A sentence is something that starts after a period, question mark,
// exclamation followed by a space. This is only not true for the first
// sentence
//
// REGEX to capture this
// (?:^|(?:[.!?]\\s+))
// Exclude group to get the starting character OR
// anything after a period, exclamation, question mark followed by a
// whitespace (space or optional new line)
//
// ([A-Z])
// Capture group to capture all the capital characters
let regexString = "(?:^|(?:[.!?]\\s+))([A-Z])"
// Initialize the regex
let regex = try NSRegularExpression(pattern: regexString,
options: .caseInsensitive)
// Convert string to a character array
var characters = Array(self)
// Loop through all the regex matches
for match in regex.matches(in: self,
options: NSRegularExpression.MatchingOptions(),
range: NSRange(location: 0,
length: count))
as [NSTextCheckingResult]
{
// We are not looking for the match, but for the group
// For example Hello Word. JELLO word will give give us two matches
// "H" and ". J" but each of the groups within each match
// will give us what we want which is "H" and "J" so we check if we
// have a group. Look up matches and groups to learn more
if match.numberOfRanges > 1
{
// Get the range (location and length) of first group from
// the regex match
let matchedRange = match.range(at: 1)
// Get the capital character at the start of the sentence we found
let characterToReplace = characters[matchedRange.location]
// Replace the capital letter with a lower cased latter
characters[matchedRange.location]
= Character(characterToReplace.lowercased())
}
}
// Convert the processed character array back to a string if needed
return String(characters)
}
catch
{
// handle errors
print("error")
return self
}
}
}
Then it can be used:
let simpleString = "Hello world"
print("BEFORE")
print(simpleString)
print("\nAFTER")
print(simpleString.withLowerCaseSentences())
let complexString
= "Hello Word. JELLO world? WEllo Word.\n ZEllo world! MeLLow lord."
print("\nBEFORE")
print(complexString)
print("\nAFTER")
print(complexString.withLowerCaseSentences())
This gives the output:
BEFORE
Hello world
AFTER
hello world
BEFORE
Hello Word. JELLO world? WEllo Word.
ZEllo world! MeLLow lord.
AFTER
hello Word. jELLO world? wEllo Word.
zEllo world! meLLow lord.

Related

How to check if a string contains a substring within an array of strings in Swift?

I have a string "A very nice beach" and I want to be able to see if it contains any words of the substring within the array of wordGroups.
let string = "A very nice beach"
let wordGroups = [
"beach",
"waterfront",
"with a water view",
"near ocean",
"close to water"
]
First solution is for exactly matching the word or phrase in wordGroups using regex
var isMatch = false
for word in wordGroups {
let regex = "\\b\(word)\\b"
if string.range(of: regex, options: .regularExpression) != nil {
isMatch = true
break
}
}
As suggested in the comments the above loop can be replace with a shorter contains version
let isMatch = wordGroups.contains {
string.range(of: "\\b\($0)\\b", options: .regularExpression) != nil
}
Second solution is for simply text if string contains the any of the strings in the array
let isMatch2 = wordGroups.contains(where: string.contains)
So for "A very nice beach" both returns true but for "Some very nice beaches" only the second one returns true
Wasn't too sure how to interpret "to see if it contains any words of the substring within the array of wordGroups", but this solution checks to see if any words of your input string are contained in any substring of your word groups.
func containsWord(str: String, wordGroups: [String]) -> Bool {
// Get all the words from your input string
let words = str.split(separator: " ")
for group in wordGroups {
// Put all the words in the group into set to improve lookup time
let set = Set(group.split(separator: " "))
for word in words {
if set.contains(word) {
return true
}
}
}
return false
}

Regular expressions in swift

I'm bit confused by NSRegularExpression in swift, can any one help me?
task:1 given ("name","john","name of john")
then I should get ["name","john","name of john"]. Here I should avoid the brackets.
task:2 given ("name"," john","name of john")
then I should get ["name","john","name of john"]. Here I should avoid the brackets and extra spaces and finally get array of strings.
task:3 given key = value // comment
then I should get ["key","value","comment"]. Here I should get only strings in the line by avoiding = and //
I have tried below code for task 1 but not passed.
let string = "(name,john,string for user name)"
let pattern = "(?:\\w.*)"
do {
let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let matches = regex.matches(in: string, options: [], range: NSRange(location: 0, length: string.utf16.count))
for match in matches {
if let range = Range(match.range, in: string) {
let name = string[range]
print(name)
}
}
} catch {
print("Regex was bad!")
}
Thanks in advance.
RegEx in Swift
These posts might help you to explore regular expressions in swift:
Does a string match a pattern?
Swift extract regex matches
How can I use String slicing subscripts in Swift 4?
How to use regex with Swift?
Swift 3 - How do I extract captured groups in regular expressions?
How to group search regular expressions using swift?
Task 1 & 2
This expression might help you to match your desired outputs for both Task 1 and 2:
"(\s+)?([a-z\s]+?)(\s+)?"
Based on Rob's advice, you could much reduce the boundaries, such as the char list [a-z\s]. For example, here, we can also use:
"(\s+)?(.*?)(\s+)?"
or
"(\s+)?(.+?)(\s+)?"
to simply pass everything in between two " and/or space.
RegEx
If this wasn't your desired expression, you can modify/change your expressions in regex101.com.
RegEx Circuit
You can also visualize your expressions in jex.im:
JavaScript Demo
const regex = /"(\s+)?([a-z\s]+?)(\s+)?"/gm;
const str = `"name","john","name of john"
"name"," john","name of john"
" name "," john","name of john "
" name "," john"," name of john "`;
const subst = `\n$2`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Task 3
This expression might help you to design an expression for the third task:
(.*?)([a-z\s]+)(.*?)
const regex = /(.*?)([a-z\s]+)(.*?)/gm;
const str = `key = value // comment
key = value with some text // comment`;
const subst = `$2,`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Separate the string by non alpha numeric characters except white spaces. Then trim the elements with white spaces.
extension String {
func words() -> [String] {
return self.components(separatedBy: CharacterSet.alphanumerics.inverted.subtracting(.whitespaces))
.filter({ !$0.isEmpty })
.map({ $0.trimmingCharacters(in: .whitespaces) })
}
}
let string1 = "(name,john,string for user name)"
let string2 = "(name, john,name of john)"
let string3 = "key = value // comment"
print(string1.words())//["name", "john", "string for user name"]
print(string2.words())//["name", "john", "name of john"]
print(string3.words())//["key", "value", "comment"]
Here I have done with after understanding all of above comments.
let text = """
Capturing and non-capturing groups are somewhat advanced topics. You’ll encounter examples of capturing and non-capturing groups later on in the tutorial
"""
extension String {
func rex (_ expr : String)->[String] {
return try! NSRegularExpression(pattern: expr, options: [.caseInsensitive])
.matches(in: self, options: [], range: NSRange(location: 0, length: self.count))
.map {
String(self[Range($0.range, in: self)!])
}
}
}
let r = text.rex("(?:\\w+-\\w+)") // pass any rex
A single pattern, works for test:1...3, in Swift.
let string =
//"(name,john,string for user name)" //test:1
//#"("name"," john","name of john")"# //test:2
"key = value // comment" //test:3
let pattern = #"(?:\w+)(?:\s+\w+)*"# //Swift 5+ only
//let pattern = "(?:\\w+)(?:\\s+\\w+)*"
do {
let regex = try NSRegularExpression(pattern: pattern)
let matches = regex.matches(in: string, range: NSRange(0..<string.utf16.count))
let matchingWords = matches.map {
String(string[Range($0.range, in: string)!])
}
print(matchingWords) //(test:3)->["key", "value", "comment"]
} catch {
print("Regex was bad!")
}
Let’s consider:
let string = "(name,José,name is José)"
I’d suggest a regex that looks for strings where:
It’s the substring either after the ( at the start of the full string or after a comma, i.e., look behind assertion of (?<=^\(|,);
It’s the substring that does not contain , within it, i.e., [^,]+?;
It’s the substring that is terminated by either a comma or ) at the end of the full string, i.e., look ahead assertion of (?=,|\)$), and
If you want to have it skip white space before and after the substrings, throw in the \s*+, too.
Thus:
let pattern = #"(?<=^\(|,)\s*+([^,]+?)\s*+(?=,|\)$)"#
let regex = try! NSRegularExpression(pattern: pattern)
regex.enumerateMatches(in: string, range: NSRange(string.startIndex..., in: string)) { match, _, _ in
if let nsRange = match?.range(at: 1), let range = Range(nsRange, in: string) {
let substring = String(string[range])
// do something with `substring` here
}
}
Note, I’m using the Swift 5 extended string delimiters (starting with #" and ending with "#) so that I don’t have to escape my backslashes within the string. If you’re using Swift 4 or earlier, you’ll want to escape those back slashes:
let pattern = "(?<=^\\(|,)\\s*+([^,]+?)\\s*+(?=,|\\)$)"

How can I count the number of sentences in a given text in Swift? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I wanted to create a playground that would count the number of sentences of a given text.
let input = "That would be the text . it hast 3. periods. "
func sentencecount() {
let periods = CharacterSet.whitespacesAndNewlines.union(.punctuationCharacters)
let periods = input.components(separatedBy: spaces)
let periods2 = Int (words.count)
print ("The Average Sentence length is \(periods2)")
}
sentencecount()
You can use enumerateSubstrings(in: Range) and use option .bySentences:
let input = "Hello World !!! That would be the text. It hast 3 periods."
var sentences: [String] = []
input.enumerateSubstrings(in: input.startIndex..., options: .bySentences) { (string, range, enclosingRamge, stop) in
sentences.append(string!)
}
An alternative is to use an array of Substrings instead of Strings:
var sentences: [Substring] = []
input.enumerateSubstrings(in: input.startIndex..., options: .bySentences) { (string, range, enclosingRamge, stop) in
sentences.append(input[range])
}
print(sentences) // "["Hello World !!! ", "That would be the text. ", "It hast 3 periods."]\n"
print(sentences.count) // "3\n"
This should work :
let input = "That would be the text . it hast 3. periods. "
let occurrencies = input.characters.filter { $0 == "." || $0 == "?" }.count
print(occurrencies)
//result 3
Just add the character in charset by which you are going to differentiate your sentences:
I am assuming ? . , for now:
let input = "That would be the text. it hast 3? periods."
let charset = CharacterSet(charactersIn: ".?,")
let arr = input.components(separatedBy: charset)
let count = arr.count - 1
Here arr would be:
["That would be the text", " it hast 3", " periods", ""]
decrease count by 1, to get actual sentences.
Note: If you don't want to consider " , " then remove it from charset.
As far as i can see that you need to split them using . and trimming whitespaces as the following:
func sentencecount () {
let result = input.trimmingCharacters(in: .whitespaces).split(separator: ".")
print ("The Average Sentence length is \(result.count)") // 3
}
Good luck!

Get the string up to a specific character

var hello = "hello, how are you?"
var hello2 = "hello, how are you #tom?"
i want to delete every letter behind the # sign.
result should be
var hello2 = "hello, how are you #tom?"
->
hello2.trimmed()
print(hello2.trimmed())
-> "hello, how are you"
Update
As i want to use it to link multiple users and replace the space behind #sign with the correct name, I always need the reference to the latest occurrence of the #sign to replace it.
text3 = "hey i love you #Tom #Marcus #Peter"
Example what the final version should look like
to start off
var text = "hello #tom #mark #mathias"
i want to always get the index of the latest # sign in the text
Expanding on #appzYourLife answer, the following will also trim off the whitespace characters after removing everything after the # symbol.
import Foundation
var str = "hello, how are you #tom"
if str.contains("#") {
let endIndex = str.range(of: "#")!.lowerBound
str = str.substring(to: endIndex).trimmingCharacters(in: .whitespacesAndNewlines)
}
print(str) // Output - "hello, how are you"
UPDATE:
In response to finding the last occurance of the # symbol in the string and removing it, here is how I would approach it:
var str = "hello, how are you #tom #tim?"
if str.contains("#") {
//Reverse the string
var reversedStr = String(str.characters.reversed())
//Find the first (last) occurance of #
let endIndex = reversedStr.range(of: "#")!.upperBound
//Get the string up to and after the # symbol
let newStr = reversedStr.substring(from: endIndex).trimmingCharacters(in: .whitespacesAndNewlines)
//Store the new string over the original
str = String(newStr.characters.reversed())
//str = "hello, how are you #tom"
}
Or looking at #appzYourLife answer use range(of:options:range:locale:) instead of literally reversing the characters
var str = "hello, how are you #tom #tim?"
if str.contains("#") {
//Find the last occurrence of #
let endIndex = str.range(of: "#", options: .backwards, range: nil, locale: nil)!.lowerBound
//Get the string up to and after the # symbol
let newStr = str.substring(from: endIndex).trimmingCharacters(in: .whitespacesAndNewlines)
//Store the new string over the original
str = newStr
//str = "hello, how are you #tom"
}
As an added bonus, here is how I would approach removing every # starting with the last and working forward:
var str = "hello, how are you #tom and #tim?"
if str.contains("#") {
while str.contains("#") {
//Reverse the string
var reversedStr = String(str.characters.reversed())
//Find the first (last) occurance of #
let endIndex = reversedStr.range(of: "#")!.upperBound
//Get the string up to and after the # symbol
let newStr = reversedStr.substring(from: endIndex).trimmingCharacters(in: .whitespacesAndNewlines)
//Store the new string over the original
str = String(newStr.characters.reversed())
}
//after while loop, str = "hello, how are you"
}
let text = "hello, how are you #tom?"
let trimSpot = text.index(of: "#") ?? text.endIndex
let trimmed = text[..<trimSpot]
Since a string is a collection of Character type, it can be accessed as such. The second line finds the index of the # sign and assigns its value to trimSpot, but if it is not there, the endIndex of the string is assigned through the use of the nil coalescing operator
??
The string, or collection of Characters, can be provided a range that will tell it what characters to get. The expression inside of the brackets,
..<trimSpot
is a range from 0 to trimSpot-1. So,
text[..<trimSpot]
returns an instance of type Substring, which points at the original String instance.
You need to find the range of the "#" and then use it to create a substring up to the index before.
import Foundation
let text = "hello, how are you #tom?"
if let range = text.range(of: "#") {
let result = text.substring(to: range.lowerBound)
print(result) // "hello, how are you "
}
Considerations
Please note that, following the logic you described and using the input text you provided, the output string will have a blank space as last character
Also note that if multiple # are presente in the input text, then the first occurrence will be used.
Last index [Update]
I am adding this new section to answer the question you posted in the comments.
If you have a text like this
let text = "hello #tom #mark #mathias"
and you want the index of the last occurrency of "#" you can write
if let index = text.range(of: "#", options: .backwards)?.lowerBound {
print(index)
}
Try regular expressions, they are much safer (if you know what you are doing...)
let hello2 = "hello, how are you #tom, #my #next #victim?"
let deletedStringsAfterAtSign = hello2.replacingOccurrences(of: "#\\w+", with: "", options: .regularExpression, range: nil)
print(deletedStringsAfterAtSign)
//prints "hello, how are you , ?"
And this code removes exactly what you need and leaves the characters after the strings clear, so you can see the , and ? still being there. :)
EDIT: what you asked in comments to this answer:
let hello2 = "hello, how are you #tom, #my #next #victim?"
if let elementIwannaAfterEveryAtSign = hello2.components(separatedBy: " #").last
{
let deletedStringsAfterAtSign = hello2.replacingOccurrences(of: "#\\w+", with: elementIwannaAfterEveryAtSign, options: .regularExpression, range: nil)
print(deletedStringsAfterAtSign)
//prints hello, how are you victim?, victim? victim? victim??
}

How to capitalize first word in every sentence with Swift

By taking into account of user locale, how can I capitalize the first word of each sentence in a paragraph? What I want to achieve is no matter the case inside the sentence, the first letter of each word will be uppercase and the rest will be lowercase. I can do only one sentence by first converting everything to lower case, then get first letter and make uppercase and finally add them up together. My question is different than How to capitalize each word in a string using Swift iOS since I don't want to capitalize each word. I just want to capitalize the first word of each sentence. capitalizedString turns
"this is first sentence. this is second sentence."
to
"This Is First Sentence. This Is Second Sentence."
What I want is
"This is first sentence. This is second sentence."
My question is also different than Capitalise first letter of every sentence Since #rintaro's code doesn't work on my below example. It keeps capital letters in original text intact. With #rintaro's code;
before
"someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ"
after
"SomeSentenceWith UTF text İŞğĞ. AnotherSentenceğüÜğ."
What I want to achieve,
"Somesentencewith utf text işğğ. Anothersentenceğüüğ."
My code below can only do partial conversion.
var description = "someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ"
description = description.lowercaseStringWithLocale(NSLocale.currentLocale())
let first = description.startIndex
let rest = advance(first,1)..<description.endIndex
let capitalised = description[first...first].uppercaseStringWithLocale(NSLocale.currentLocale()) + description[rest]
I will really appreciate if you can please read my question carefully, since this is the third time I am editing the question. I am really sorry if I couldn't ask it clearly since I am not a native speaker. So even though #rintaro answered similar question, his answer doesn't solve my problem. #martin-r suggests a Objective-C answer which again doesn't solve the problem I have. There were another user eric something who also suggested another answer but deleted afterwards. I just can't understand why several people suggest different answer which doesn't answer my question.
Try:
let str = "someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ"
var result = ""
str.uppercaseString.enumerateSubstringsInRange(indices(str), options: .BySentences) { (sub, _, _, _) in
result += sub[sub.startIndex ... sub.startIndex]
result += sub[sub.startIndex.successor() ..< sub.endIndex].lowercaseString
}
println(result) // -> "Somesentencewith utf text i̇şğğ. Anothersentenceğüüğ"
ADDED: Swift2
let str = "someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ"
var result = ""
str.uppercaseString.enumerateSubstringsInRange(str.characters.indices, options: .BySentences) { (sub, _, _, _) in
result += String(sub!.characters.prefix(1))
result += String(sub!.characters.dropFirst(1)).lowercaseString
}
print(result)
Updating #rintaro's code for Swift 3:
let str = "someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ"
var result = ""
str.uppercased().enumerateSubstrings(in: str.startIndex..<str.endIndex, options: .bySentences) { (sub, _, _, _) in
result += String(sub!.characters.prefix(1))
result += String(sub!.characters.dropFirst(1)).lowercased()
}
print(result)
You can use Regular Expressions to achieve this. I'm adding this function as a String extension so it will be trivial to call in the future:
extension String {
func toUppercaseAtSentenceBoundary() -> String {
var string = self.lowercaseString
var capacity = string.utf16Count
var mutable = NSMutableString(capacity: capacity)
mutable.appendString(string)
var error: NSError?
if let regex = NSRegularExpression(
pattern: "(?:^|\\b\\.[ ]*)(\\p{Ll})",
options: NSRegularExpressionOptions.AnchorsMatchLines,
error: &error
) {
if let results = regex.matchesInString(
string,
options: NSMatchingOptions.allZeros,
range: NSMakeRange(0, capacity)
) as? [NSTextCheckingResult] {
for result in results {
let numRanges = result.numberOfRanges
if numRanges >= 1 {
for i in 1..<numRanges {
let range = result.rangeAtIndex(i)
let substring = mutable.substringWithRange(range)
mutable.replaceCharactersInRange(range, withString: substring.uppercaseString)
}
}
}
}
}
return mutable
}
}
var string = "someSentenceWith UTF text İŞğĞ. anotherSentenceğüÜğ.".toUppercaseAtSentenceBoundary()
I wrote this extension in Swift 3 according to #Katy's code
extension String {
func toUppercaseAtSentenceBoundary() -> String {
var result = ""
self.uppercased().enumerateSubstrings(in: self.startIndex..<self.endIndex, options: .bySentences) { (sub, _, _, _) in
result += String(sub!.characters.prefix(1))
result += String(sub!.characters.dropFirst(1)).lowercased()
}
return result as String
}
}
How to use:
let string = "This is First sentence. This is second Sentence.".toUppercaseAtSentenceBoundary()
print(string) /* Output: "This is first sentence. This is second sentence." */