String treatment in Javascript

Strings in Javascript: In this article you will find all the information and examples of the treatment of strings in Javascript or character strings. String class and objects, string utility functions, etc.

In JavaScript, text type variables are objects of the String class. This means that each of the variables that we create of type text have a series of properties and methods. We remember that the properties are the characteristics, such as the length in characters of the string and the methods are functionalities, such as extracting a substring or putting the text in upper case.

To create an object of the String class, all you have to do is assign a text to a variable. The text is enclosed in quotation marks, as we have already seen in the syntax chapters. You can also create a string object with the new operator, which we’ll see later. The only difference is that in versions of Javascript 1.0 new will not work to create the Strings.

String Properties

Length

The String class only has one property: length, which stores the number of characters in the String.

String methods

Objects of the String class have a number of methods to do many interesting things. First we are going to see a list of the most interesting methods and then we are going to see another list of less useful methods.

charAt(index)

Returns the character at the indicated position as index. The positions of a string start at 0.

indexOf(character,from)

Returns the position of the first occurrence of the character indicated by parameter in a string. If it doesn’t find the character in the string it returns -1. The second parameter is optional and is used to indicate from which position you want the search to start.

lastIndexOf(character,from)

Searches for a character position exactly the same as the indexOf function but from the end instead of the beginning. The second parameter indicates the number of characters to search from, just like in indexOf.

See also  CSS Media Queries

replace(substring_to_search,newStr)

Implemented in Javascript 1.2, it is used to replace portions of a string’s text with other text, for example, we could use it to replace all occurrences of the “xxx” substring with “yyy”. The method does not replace on the string, but instead returns a result of doing that replacement. Accepts regular expressions such as substring to search for.

split(separator)

This method is only compatible with javascript 1.1 onwards. It is used to create a vector from a String in which each element is the part of the String that is separated by the separator indicated by parameter.

substring(start,end)

Returns the substring starting at the start character and ending at the end character. If we swap the start and end parameters it also works. It simply gives us the substring between the minor and major characters.

toLowerCase()

Puts all characters in a string in lowercase.

toUpperCase()

Puts all characters in a string in uppercase.

toString()

This method is found by all objects and is used to convert them to strings.

So far we have seen the methods that will help us deal with strings. Now we are going to see other methods that are less useful, but they must be indicated so that they are recorded. They all serve to apply styles to a text and it is as if we used HTML tags. Let’s see how.

anchor(name)

It converts a string of characters into an anchor (place to direct a link) using as the name attribute of the tag what it receives as a parameter.

big()

Increases the font size of the string. It is as if we put the tag at the beginning of a string and at the end.

See also  Git deployment fails due to changes made to composer.lock What to do?

blink()

To make the string text blink is like using the tag. It only works for Netscape.

bold()

As if we used the tag.

fixed()

To use a monospace font, tag.

fontColor(color)

Sets the font to that color. How to use the tag .

fontSize(size)

Sets the font to the indicated size. As if we were to use the tag with the size attribute.

italics()

Italicizes the font. tag.

link(url)

Puts the text as a link to the specified URL. It is as if we were to use the tag with the href attribute indicated as a parameter.

small()

It’s like using the tag

strike()

How to use the tag, which is used to make the text appear crossed out.

sub()

Updates the text as if the tag, for subscript, were being used.

His p()

As if we used the tag , superscript.

Examples of operation of String class objects

So far we have seen many interesting methods of String type elements, but it will not hurt to go into practice to understand everything much better. So, let’s look at some examples of how the methods and properties of the String object are used.

String example 1

We are going to write the content of a string with a separator character (“-“) between each of the characters in the string.

var myString = “Hello Friends” var result = “” for (i=0;i First we create two variables, one with the string to be traversed and the other with an empty string, where we will store the result. Then we make a loop that goes from the first to the penultimate character of the string -we use the length property to know the number of characters in the string- and in each iteration we place a character of the string followed by a separator character “-“. Since we still have the last character to place, we put it on the next line after the loop. We use the charAt function to access the positions of the string. Finally we print the result on the page.

String example 2

Let’s make a script that breaks a string into two halves and prints them to the screen. The halves will be equal, as long as the string has an even number of characters. In the event that the number of characters is odd, it will not be possible to do the exact half, but we will split the string as close as possible to half.

var myString = “0123456789” var half1,half2 position_half = myString.length / 2 half1 = myString.substring(0,position_half) half2 = myString.substring(position_half,myString.length) document.write(half1 + “
” + half2)

The first two lines are used to declare the variables that we are going to use and initialize the string from. In the next line we find the position of the middle of the string.

The next two lines are where we do the job of putting the first half of the string in one variable and the second half in the other. To do this we use the substring method passing it as start and end in the first case from 0 to the middle and in the second from the middle to the end. To finish we print the two halves with a line break between them.

Once we know how to handle the objects of the string class, we can move on to see other of the native Javascript classes, such as the .

Loading Facebook Comments ...
Loading Disqus Comments ...