2

Goal : Multiple splits of the string, Each must have 50 characters. It would be really great if I can do it by proper word wrapping. It is okay to have the last split as less than 50 characters.

I am using this method to split a String into multiple parts. I have used a regular expression for this, which is given below.

                           "\\G\\s*(.{1,50})(?=\\s|$)"

And used this NSURLRegularExpression to find the corresponding splits.

extension String {

   func getSplits()
   {
     let pattern = try? NSRegularExpression(pattern: "\\G\\s*(.{1,50})(?=\\s|$)", options: NSRegularExpressionOptions.DotMatchesLineSeparators)

        if let matches = pattern?.matchesInString(self, options: NSMatchingOptions.ReportCompletion , range: NSMakeRange(0, self.characters.count)) as [NSTextCheckingResult]?
        {
            for (index, result) in matches.enumerate()
            {
                // Statements
            }
        }
    }
}

But the problem is that, whenever I put English text it returns me the proper splits. But whenever I put text other than English, it seems to be showing incorrect splits. As in each split must have 50 characters right? But number of characters in the output are sometimes 5 or 10 or some random number. Randomly splitting, I have a doubt? Is there anything to do with the \n character or \r? No right?

I have tried all option types of NSRegularExpressionOptions and NSMatchingOptions. Even no options, but same thing happened.

If you are testing, this string will help you, test it with this Malayalam Language text ->

കുളച്ചല്‍ തുറമുഖം നിര്‍മ്മിക്കാനുള്ള നീക്കം യുക്തിരഹിത പദ്ധതിയെന്നാണ് മുഖ്യമന്ത്രി പിണറായി വിജയന്‍ മറുപടി നല്‍കിയത്. പ്രധാനമന്ത്രിയെ നേരില്‍ കണ്ട് ഇക്...
പൊതുജനങ്ങളുടെ നികുതിപ്പണം യുക്തിരഹിതമായി ഉപയോഗിക്കുന്നതിനുള്ള തെളിവാണ് കുളച്ചല്‍. 1000 ദിവസം കൊണ്ട് മുന്‍നിശ്ചയപ്രകാരം പദ്ധതി പൂര്‍ത്തിയാക്കും. തുറമുഖ...

Do you have any thoughts on this?

  • How many graphemes vs. characters? – Feldur Jul 15 '16 at 09:18
  • @Feldur As in? I didn't understand. –  Jul 15 '16 at 09:21
  • The grapheme's are the number of characters that you would see in the characters view. The characters could be a significantly larger number as they will need to combine to form the complete grapheme. There are as well, multiple ways the characters combine to give you a given grapheme – Feldur Jul 15 '16 at 21:36

0 Answers0