Swift: string.characters.count returns invalid number for arabic string

I have the following text written in Arabic, and when I call text.characters.count , it returns 298 characters instead of a real number, which is 300.

Text:

هنالك العديد من الأنواع المتوفرة لنصوص لوريم إيبسوم, ولكن الغالبية تم تعديلها بشكل ما عبر إدخالأالوال ت إن كنت تريد أن تستخدم نص لوريم إيبسوم ما, عليك أن تتحقق أولا أن ليس هناك أي كلمات أو عبارات محرفأاأ بينما تعمل جميع مولدات نصوص ا

with the mention that there is no surrounding space, before and after the text.

utf8.characters.count also returns the same invalid number.

How can I get the right amount of characters given by such a string?

+7
string ios swift
source share
2 answers

Getting a unicode scalar count should give the expected result:

 let myString = "هنالك العديد من الأنواع المتوفرة لنصوص لوريم إيبسوم، ولكن الغالبية تم تعديلها بشكل ما عبر إدخال بعض النوادر أو الكلمات العشوائية إلى النص. إن كنت تريد أن تستخدم نص لوريم إيبسوم ما، عليك أن تتحقق أولاً أن ليس هناك أي كلمات أو عبارات محرجة أو غير لائقة مخبأة في هذا النص. بينما تعمل جميع مولّدات نصوص ا" myString.unicodeScalars.count // 300 المتوفرة لنصوص لوريم إيبسوم, ولكن الغالبية تم تعديلها بشكل ما عبر إدخال بعض النوادر أو الكلمات العشوائية إلى النص. إن كنت تريد أن تستخدم نص لوريم إيبسوم ما, عليك أن تتحقق أولا أن ليس هناك أي كلمات أو عبارات let myString = "هنالك العديد من الأنواع المتوفرة لنصوص لوريم إيبسوم، ولكن الغالبية تم تعديلها بشكل ما عبر إدخال بعض النوادر أو الكلمات العشوائية إلى النص. إن كنت تريد أن تستخدم نص لوريم إيبسوم ما، عليك أن تتحقق أولاً أن ليس هناك أي كلمات أو عبارات محرجة أو غير لائقة مخبأة في هذا النص. بينما تعمل جميع مولّدات نصوص ا" myString.unicodeScalars.count // 300 

As mentioned in Swift - Strings and characters :

Behind the scenes, the string type Swifts is built from Unicode scalar values . A Unicode scanner is a unique 21-bit number for a character or modifier, such as U + 0061 for LATIN SMALL LETTER A ("a"), or U + 1F425 for FRONT-FACING BABY CHICK ("🐥").

but

No matter what result you expect, counting the "character" (delimiters) such as "Fat-ha", "damma", "kasra" as a split character, probably gives the wrong result .

For example: if you tried to check the number of words "أولا", you will notice that:

 let myString = "أولاً" myString.characters.count // 4 myString.unicodeScalars.count // 5 

as you can see, the TanweenFat-ha character is not considered a separate character unless you consider its unicodeScalars value.

As you already mentioned, it seems that charactercountonline.com considers " character " (delimiters) to be independent characters, which should be logical for people who do not speak Arabic, but that would be a wrong count.


Note For non-Arab speaking speakers:

The word "أولا" contains a decimal separator called "Fat-hatan" or "Tanween Fat-h", this separator should not be considered as a separate character related to Arabic grammars; The purpose of its use is to indicate how the word should be written. This logic should be obvious to people who speak Arabic that the word "أولا" contains four characters, but not computers, when it is associated with counting!

+4
source share

[...] when I call text.characters.count, it returns 298 characters instead of a real number, which is 300.

It all comes down to the definition of a symbol (of which there are several).

Swift's definition is slightly different from most other computer languages, because it defines a symbol as a single extended cluster of graphemes :

An extended grapheme cluster is a sequence of one or more Unicode scanners that (when combined) create one human-readable character.

Therefore, when working with the "number of characters", it is important to think about what you really want to know: is this what a person will perceive as characters, or is it about some (computer) coding?

Without a correct definition, there is no “right” answer.

+2
source share

All Articles