Saturday, July 14, 2018

Typescript String - Complete tutorials with examples

Learn String object Data type

Learn Typescript String tutorials
The string contains a group of characters saved under String object variable name. You need to know basic operations on String How it works? In this tutorial, we will walk through the different methods and examples.

Basic syntax Of String Object creation


var variable_name = new string('CloudHadoop')

there are many ways we can create a string object in typescript

var cloudVariableName= new string('CloudHadoop');
let newStringExample= '123';

a string is a one of datatype in typescript. Don't confuse with the String object. The difference between string and String is String is a type in javascript and string is of typescript

The empty or Null check of a String in typescript

return true if String is Numeric String also contains numbers in single quotes. To check String is Numeric, first, return the string format then check using isNaN method which checks for the valid number

function checkSuppliedStringisNumeric(strParameter: string | number): boolean
{
   return !isNaN(Number(strParameter.toString()));
}
console.log(checkSuppliedStringisNumeric('123')) // outputs true
console.log(checkSuppliedStringisNumeric('123abc')) // outputs false
console.log(checkSuppliedStringisNumeric('abc')) // outputs false*+++

How to remove whitespaces in String object?. 

This is the common requirement for deleting whitespaces from String object we can easily do in many ways. the string provides trim() method which removes from first and last characters of a word, another way is using regular express which removes complete spaces



console.log("cloud hadoop".replace(/\s/g, "")) // outputs cloudhadoop
console.log("cloud hadoop".trim()) // outputs cloud hadoop

console.log(" cloud hadoop ".replace(/\s/g, "")) // outputs cloudhadoop
console.log(" cloud hadoop ".trim()) // outputs cloud hadoop

String for each iteration Example. 

First, we will see how String array is created in typescript. string type can be declared after variable followed by a semicolon iteration of strings can be achieved using forEach or for-in loop functionality


var msgs: string[] = ['string1', 'string2', 'string3'];
var output;
// ForEach String array  iteration example
msgs.forEach(function(entry) {
     console.log(entry);
});
// For-In String array iteration example
for(const msg in msgs) {
        console.log(msgs[msg]);
}
// For-Of String array iteration example
for (const m of msgs) {
    console.log(m);
}
// plain For loop String  array iteration example
for (let i = 0; i < msgs.length; ++i) {
    console.log (msgs[i]);
}

There are many ways to loop String array. for-in and plain for loop using index mechanism to iterate elements and order not guaranteed. whereas forEach and forOf iterates over each String array object.

typescript Split String into an array of Substrings example

Using substring, we can split a string using spaces


var message = "[cloud,hadoop,examples,tutorials]";
function splitStringIntoArray(input) {
 var messages = input.substring(1, input.length-1).split(", ");
 return messages;
}
document.body.innerHTML = splitStringIntoArray(message)[0] + " " + splitStringIntoArray(message)[1] + " " + splitmessages(message)[2];

How to convert String to a Boolean example. 

strings contain true or false values. if we passed string boolean values return as a string, else return undefined. Here undefined means not able to parse to boolean type



function transferToBoolean(input: string): boolean | undefined {
    try {
        return JSON.parse(input);
    }
    catch (e) {
        return undefined;
    }
}

console.log(transferToBoolean("true")); // true
console.log(transferToBoolean("false")); // false
console.log(transferToBoolean("invalid")); // undefined


String type methods

Method Description
concat Joining the two strings and return one output string
toLowerCase return the lowercase of output string
toUpperCase return the uppercase of the output string
toString returns the string
substring returns the substring of starting index to end index
charAt return character at the specified index in a string
slice return the portion of a string
Please also see other tutorials from Typescript language

Typescript Tutorials and Examples

Install and Hello World Example
Enum Tutorials and Examples 
String Tutorials and Examples 

Related article


EmoticonEmoticon