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.