11. More about strings
To split a string, search it, and trim the spaces from both ends.
Explanation
Section titled “Explanation”تقسيم(ن, فاصل)(split) splits the string into a list of strings at every place the separator appears.قص_الفراغات(ن)(trim) returns the string without spaces at its start and end.يحوي(ن, جزء)(contains) answers: does the part appear in the string? Andيبدأ_بـ(ن, بادئة)(starts with) answers: does the string start with it?
Example
Section titled “Example”سطر = " تفاح، موز، عنب "قطع = تقسيم(قص_الفراغات(سطر), "، ")اطبع(قطع, " ", يحوي(سطر, "موز"), " ", يبدأ_بـ(قطع[0], "تف"))Common mistakes
Section titled “Common mistakes”Thinking that طول (length) counts bytes: it counts code points, and diacritics count too. A code point is the counting unit for strings (glossary).
Exercise
Section titled “Exercise”Trim the spaces from both ends of « ميم لغة عربية صغيرة », split it into words, and print how many there are and the words that start with the letter «ع».
Show the solution
جملة = " ميم لغة عربية صغيرة "كلمات = تقسيم(قص_الفراغات(جملة), " ")اطبع("عدد الكلمات: ", طول(كلمات))لكل ك في كلمات: اذا يبدأ_بـ(ك, "ع"): اطبع("تبدأ بالعين: ", ك)حرر(كلمات)