dreamsys software

JavaScript Shell Scripting Tutorial

String Manipulation (trim, substring, etc)

One of the powerful features of JavaScript is its ease of use in string manipulation. Operations such as indexOf, substring, split, trim and other common string manipulations are easy under JavaScript.

Here we will give a simple example that shows many of these operations.

#!/usr/bin/env node

var strUsers = 'rpulley  ,    jsmith, svai,  jsatriani    ,ymalmsteen    ';
var arrUsers = strUsers.split(',');

for (i in arrUsers) {
    var user = arrUsers[i];
    var trimUser = user.trim();

    var firstInitial = trimUser.substring(0, 1);
    var lastInitial = trimUser.substring(1, 2);
    var lastName = trimUser.substring(1);
    var index = trimUser.indexOf(lastName);

    console.log('User: \'' + user + '\'');
    console.log('Trim: \'' + trimUser + '\'');
    console.log('First Initial: ' + firstInitial.toUpperCase());
    console.log('Last Initial: ' + lastInitial.toUpperCase());
    console.log('Last Name: ' + lastName);
    console.log('Index of Last Name: ' + index);

So in the first line, we have a simple string, which has a list of User IDs separated by commas. These user IDs are in the format first initial followed by last name, there is also some noise in the form of spaces around some of the names. This happens very often in programming situations, especially when dealing with databases. The common way to deal with this is to "trim" the strings. The first thing we need to do, however, is to first get this string into an array, so we use the string split function to split the string into several substrings based on a given token, which we give the ',' as a parameter for.

Now we have an array of user strings in the variable arrUsers. Now that we have this array, we can simply use a for loop to iterate through all of the users. The first thing we need to do with each user is get rid of the whitespace noise around the user ID. This is done with JavaScript's string trim function. This function will strip any whitespace off the start and end of the string.

Now that we have the user ID cleaned of the whitespace, we will want to substring to get the first initial, last initial and last name from the user ID. The first parameter of substring is the start index and the second optional parameter is the end index.

When we print the first and last initials, we want these letters to be capitalized, so we use JavaScripts's string toUpperCase function to convert the string to uppercase. Likewise there is a toLowerCase function if you want to convert a string to lower case.

Additionally, we see that the indexOf function is used for finding the occurance of a string within a string and returning the index of that string.

There are some essential data structures which are supported in JavaScript, such as Lists and Maps, which we will cover in the next section.

Prev (Classes) | Next (Lists & Maps)

Blog Entries
Blob Entry 1
Blob Entry 2
Blob Entry 3
Blob Entry 4
Blob Entry 5
Blob Entry 6