Typescript String - Complete tutorials with examples
- Admin
- Sep 20, 2023
- Typescript
You can check another post on Fix for Object is possibly null
typescript String object Data type
The string contains a group of characters saved under the 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 one of the datatypes in typescript.
Don’t confuse it with the String
object.
The difference between string and String
A string is a type in javascript and a string is of typescript.
How to check The empty or Null of a String in typescript?
In this example, return true if the String is Numeric.
String also contains numbers in single quotes.
To check String is Numeric, first, return the string format then check using the isNaN
method that 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 the String object.
It is easy to do in many ways.
the string provides the 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 the String array is created in typescript.
string type declared after the variable followed by a semicolon.
iteration of strings 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 the String array. for-in and plain for loop using index mechanism to iterate elements and order not guaranteed.
whereas forEach and forOf iterate over each String array object.
typescript Split String into an array of Substrings example
Using the substring
function, 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 the string the return of a Boolean value as a string, else return undefined.
Here undefined means not able to parse to a 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
Typescript provides various inbuilt methods for the manipulation of string content
Method | Description |
---|---|
concat | Joining the two strings and returning one output string |
toLowerCase | return the lowercase of the output string |
toUpperCase | return the uppercase of the output string |
toString | returns the string content |
substring | returns the substring of starting index to the end index |
charAt | return character at the specified index in a string |
slice | return the portion of a string |
Related posts:
- Typescript Tutorials and Examples
- Install and Hello World Example
- Enum Tutorials and Examples
- String Tutorials and Examples
Conclusion
To summarize, Learned string class in typescript with methods and example use cases.