objective c - Format text from "HELLO, WORLD. HOW ARE YOU?" to "Hello, world. How are you?" in ObjC(iOS)? -
as title said, need format string of text in format like: "hello, world. how you?" "hello, world. how you?", there standard method doing in ios? or there sample code ?
thanks!
there no direct way capitalize first characters of sentences in paragraph. nsstring has method capitalizedstring capitalizes each , every word. have create own logic achieve this. can try following code.
- (nsstring *)captilizeparagraph:(nsstring *)paragraph { if ([paragraph length] == 0) return paragraph; nsarray *sentences = [paragraph componentsseparatedbystring:@". "]; nsmutablearray *capitalizedsentences = [nsmutablearray array]; (nsstring *sentence in sentences) { sentence = [sentence lowercasestring]; sentence = [sentence stringbyreplacingcharactersinrange:nsmakerange(0,1) withstring:[[sentence substringtoindex:1] uppercasestring]]; [capitalizedsentences addobject:sentence]; } nsstring *capitalizedparagrah = [capitalizedsentences componentsjoinedbystring:@". "]; return capitalizedparagrah; }
note: above code assumes sentences in paragraph ends characters ". "
(a dot , space) except last sentence(it can end character). if sentence ends other characters "? "
or "! "
, method return not-fully-formatted string.
Comments
Post a Comment