Useful string methods

This translation is incomplete. Please help translate this article from English

الآن بعدما تعلمنا أساسيات السلاسل، دعنا نبدأ في التفكير وتجربة العمليات التي يمكننا القيام بها علي هذه السلاسل، مثل إيجاد طول السلسلة، ربط السلاسل وايضًا طريقة تقسيمها الي اجزاء، ومعرفة كيف أستبدال حرف بآخر، وأشياء أخرى.

Prerequisites: Basic computer literacy, a basic understanding of HTML and CSS, an understanding of what JavaScript is.
Objective: To understand that strings are objects, and learn how to use some of the basic methods available on those objects to manipulate strings.

السلاسل ككائنات

معظم الاشياء في الجافا سكريبت هي كائنات. فعند إنشاء سلسلة، علي سبيل المثال بإستخدام :

let string = 'This is my string';

يصبح المتغير الخاص بك مثل كائن سلسلة، وبناءًا علي ذلك يصبح لديه العديد من الخصائص والأساليب المتاحة له والتي يمكن تنفيذها علية. يمكنك رؤية ذلك إذا انتقلت الي صفحة الكائن String وألقيت النظرة علي القائمة الموجودة في هذه الصفحة بالأسفل.

الان وقبل أن يبدأ عقلك في التوهان او التشتيت، لا تقلق! انت لست بحاجة لمعرفة هذه الاشياء في وقت مبكر من رحلة تعلمك للجافا سكريبت. وهناك عدد قليل من الاحتمالات لإستخدامك هذا كثيرًا والذي سننظر إلية هنا في هذا المقال.

دعنا نجرب بعض الامثلة داخل الكونسول الخاص بالمتصفح.

إيجاد طول السلسلة

هذا سهل للغاية — انت ببساطة تستخدم هذه الخاصية لإيجاد طول السلسلة length حاول إتباع الأسطر التالية :

// هنا نقوم بإنشاء متغير يحتوي علي سلسلة نصية
let browserType = 'العربية';

// هنا نقوم بكتابة اسم المتغير متبوعًا بالخاصية لمعرفة طول القيمة الموجودة داخل المتغير
browserType.length;

قيمة الخرج ستكون 7, لان كلمة "العربية" تحتوي علي 7 أحرف. وهذه الخاصية مفيدة لعدة أسباب؛ علي سبيل المثال، قد ترغب في العثور علي أطول سلسلة نصية من الاسماء حتي تتمكن من عرضها بترتيب الطول، او أخبار المستخدم ان اسم المستخدم الذي قام بإدخالة في حقل النموذج (حقل الإدخال) هو طويل جدًا. (في حالة تجاوز الإسم طول معين.)

استرجاع (استدعاء) حرف سلسلة معين

في ملاحظة ذات صلة،  يمكن إرجاع أي حرف داخل السلسلة باستخدام الاقواس المربعة — هذا يعني ادراج او تضمين الاقواس المربعة ([]) في نهاية اسم المتغيرالخاص بك. وفي داخل الاقواس ستقوم بكتابة رقم الحرف الذي تريد إرجاعه،  لذا علي سبيل المثال إذا كنت تريد إرجاع الحرف الاول فستقوم بإتباع الكود التالي:

browserType[0];

تذكر: الكمبيوتر يبدأ العد من 0 وليس1! ويمكنك استخدام هذا علي سبيل المثال للعثور علي الحرف الاول من السلسلة وترتيبها أبجديًا.

To retrieve the last character of any string, we could use the following line, combining this technique with the length property we looked at above:

browserType[browserType.length-1];

The length of "mozilla" is 7, but because the count starts at 0, the character position is 6; using  length-1 gets us the last character.

Finding a substring inside a string and extracting it

  1. Sometimes you'll want to find if a smaller string is present inside a larger one (we generally say if a substring is present inside a string). This can be done using the indexOf() method, which takes a single parameter — the substring you want to search for. Try this:
    browserType.indexOf('zilla');
    This gives us a result of 2, because the substring "zilla" starts at position 2 (0, 1, 2  — so 3 characters in) inside "mozilla". Such code could be used to filter strings. For example, we may have a list of web addresses and only want to print out the ones that contain "mozilla".
  1. This can be done in another way, which is possibly even more effective. Try the following:
    browserType.indexOf('vanilla');
    This should give you a result of -1 — this is returned when the substring, in this case 'vanilla', is not found in the main string.

    You could use this to find all instances of strings that don't contain the substring 'mozilla', or do, if you use the negation operator, as shown below. You could do something like this:
    if(browserType.indexOf('mozilla') !== -1) {
      // do stuff with the string
    }
  2. When you know where a substring starts inside a string, and you know at which character you want it to end, slice() can be used to extract it. Try the following:
    browserType.slice(0,3);
    This returns "moz" — the first parameter is the character position to start extracting at, and the second parameter is the character position after the last one to be extracted. So the slice happens from the first position, up to, but not including, the last position. In this example, since the starting index is 0, the second parameter is equal to the length of the string being returned.
     
  3. Also, if you know that you want to extract all of the remaining characters in a string after a certain character, you don't have to include the second parameter! Instead, you only need to include the character position from where you want to extract the remaining characters in a string. Try the following:
    browserType.slice(2);
    This returns "zilla" — this is because the character position of 2 is the letter z, and because you didn't include a second parameter, the substring that was returned was all of the remaining characters in the string. 

Note: The second parameter of slice() is optional: if you don't include it, the slice ends at the end of the original string. There are other options too; study the slice() page to see what else you can find out.

Changing case

The string methods toLowerCase() and toUpperCase() take a string and convert all the characters to lower- or uppercase, respectively. This can be useful for example if you want to normalize all user-entered data before storing it in a database.

Let's try entering the following lines to see what happens:

let radData = 'My NaMe Is MuD';
radData.toLowerCase();
radData.toUpperCase();

Updating parts of a string

You can replace one substring inside a string with another substring using the replace() method. This works very simply at a basic level, although there are some advanced things you can do with it that we won't go into yet.

It takes two parameters — the string you want to replace, and the string you want to replace it with. Try this example:

browserType.replace('moz','van');

This returns "vanilla" in the console. But if you check the value of browserType, it is still "mozilla'. To actually update the value of the browserType variable in a real program, you'd have to set the variable value to be the result of the operation; it doesn't just update the substring value automatically. So you'd have to actually write this: browserType = browserType.replace('moz','van');

Active learning examples

In this section we'll get you to try your hand at writing some string manipulation code. In each exercise below, we have an array of strings, and a loop that processes each value in the array and displays it in a bulleted list. You don't need to understand arrays or loops right now — these will be explained in future articles. All you need to do in each case is write the code that will output the strings in the format that we want them in.

Each example comes with a "Reset" button, which you can use to reset the code if you make a mistake and can't get it working again, and a "Show solution" button you can press to see a potential answer if you get really stuck.

Filtering greeting messages

In the first exercise we'll start you off simple — we have an array of greeting card messages, but we want to sort them to list just the Christmas messages. We want you to fill in a conditional test inside the if( ... ) structure, to test each string and only print it in the list if it is a Christmas message.

  1. First think about how you could test whether the message in each case is a Christmas message. What string is present in all of those messages, and what method could you use to test whether it is present?
  2. You'll then need to write a conditional test of the form operand1 operator operand2. Is the thing on the left equal to the thing on the right? Or in this case, does the method call on the left return the result on the right?
  3. Hint: In this case it is probably more useful to test whether the method call isn't equal to a certain result.

Fixing capitalization

In this exercise we have the names of cities in the United Kingdom, but the capitalization is all messed up. We want you to change them so that they are all lower case, except for a capital first letter. A good way to do this is to:

  1. Convert the whole of the string contained in the input variable to lower case and store it in a new variable.
  2. Grab the first letter of the string in this new variable and store it in another variable.
  3. Using this latest variable as a substring, replace the first letter of the lowercase string with the first letter of the lowercase string changed to upper case. Store the result of this replace procedure in another new variable.
  4. Change the value of the result variable to equal to the final result, not the input.

Note: A hint — the parameters of the string methods don't have to be string literals; they can also be variables, or even variables with a method being invoked on them.

Making new strings from old parts

In this last exercise, the array contains a bunch of strings containing information about train stations in the North of England. The strings are data items that contain the three-letter station code, followed by some machine-readable data, followed by a semicolon, followed by the human-readable station name. For example:

MAN675847583748sjt567654;Manchester Piccadilly

We want to extract the station code and name, and put them together in a string with the following structure:

MAN: Manchester Piccadilly

We'd recommend doing it like this:

  1. Extract the three-letter station code and store it in a new variable.
  2. Find the character index number of the semicolon.
  3. Extract the human-readable station name using the semicolon character index number as a reference point, and store it in a new variable.
  4. Concatenate the two new variables and a string literal to make the final string.
  5. Change the value of the result variable to equal to the final string, not the input.

Test your skills!

You've reached the end of this article, but can you remember the most important information? You can find some further tests to verify that you've retained this information before you move on — see Test your skills: Strings.

Conclusion

You can't escape the fact that being able to handle words and sentences in programming is very important — particularly in JavaScript, as websites are all about communicating with people. This article has given you the basics that you need to know about manipulating strings for now. This should serve you well as you go into more complex topics in the future. Next, we're going to look at the last major type of data we need to focus on in the short term — arrays.

In this module