Javascript String Variable Types

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/javascript-string-variable-types/
PINTEREST
PINTEREST
INSTAGRAM

While developing Javascript applications we generally need some type that can hold text and string data. We call this type of variable as string. String can hold names, street info, paragraph, explanation etc. String is the type used for text data. String variables can be defined various ways.

  • These are string variables

  • val is a String object the very similar to string variable

There are methods to use with string data type. String data type is wrapped as String object in Javascript to provide this methods.

Get The Length of String

To get length of the string is easy as below. Length is equal to the number of characters include spaces.

Code:

  • length provides the length of string

Output:

  • As we see second line of output is 7 where second string have a space at the end

Find Index of String

If we are looking a characters position in the string we can use indexOf function

Code:

Output:

  • If the char is not exists in the string -1 is returned.

Combine Strings

As we know strings cannot be changed after initialized so to add some string concat  can be used

LEARN MORE  Powershell Trim Operation Like Filename, String, End, Start, Spaces

Code:

Output:

Return A Char With Index

To get a character from specified index  charAt function is used. Index numbers starts from 0.

Code:

Output:

Replace String

To replace string replace method is used. There is two parameters.

Code:

  • me” is the string we want to replace
  • “you” the new string

Output:

Search String

Searching for specified string in a string. Returns the index of the search term.

Code:

Output:

Substring

There is two type of substring method. Substring is used to extract some partof string by providing index values.

Code:

  • is start index for substring
  • is end index for substring

Output:

Code:

  • is start index for substring
  • is length of the substring from start index which means we will get 2 characters
LEARN MORE  Python JSON Encoder and Decoder Tutorial with Examples

Output:

To Lower Case

All uppercase characters can be converted into lowercase characters with toLowerCase method

Code:

Output:

To Upper Case

All lowercase characters can be converted into uppercase characters with toUpperCase method

Code:

Output:

Convert To String

We may need numeric or other data types to convert string to manupilate them.

Returned object type will be string

Code:

Output:

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/javascript-string-variable-types/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)