String Object Method In TypeScript: Part 1

Introduction

 
The String object is used to manage a series of characters. The String object contains complete information about strings. In TypeScript, String objects are created with "new String()".
 
In this article, I am describing the TypeScript charAt and charCodeAt string methods.
 

chatAt() Method

 
In TypeScript, the charAt() method returns the character at the specified position in the string. Characters in a string proceed (are indexed) from left to right. The position of the first character is 0 and the position of the last character in a string is string.length - 1.
 
Syntax
  1. string.charAt(position)  
position: is a required parameter; represents the position of the character.
 
Function
 
A sample use of charAt is:
  1. charAt(str: string) {  
  2.  var position = str.charAt(0);  
  3.  var span = document.createElement("span");  
  4.  span.innerText = "charAt Method \n Display the first character of a string-> " + position + "\n";  
  5.  document.body.appendChild(span);  
  6. }  

charCodeAt() Method

 
In TypeScript the charCodeAt() method returns a number that represents the Unicode value of the character at the given position.
 
Syntax
  1. string.charCodeAt(position)  
position: is a required parameter; the position value is between 0 and the position of the last character, which is 1 less than the length of the string. The default position is 0.
 
Function
 
A sample use of charCodeAt is:
  1. charCodeAt(str: string) {  
  2.  var unicode = str.charCodeAt(str.length - 1);  
  3.  var span = document.createElement("span");  
  4.  span.style.color = "green";  
  5.  span.innerText = "charCodeAt Method \n Display the unicode of the last character in a string-> " + unicode + "\n";  
  6.  document.body.appendChild(span);  
  7. }   
The following example shows how to use charAt and charCodeAt methods in TypeScript. In this example, we get a string from the user. The charAt() method returns the character at the specified position and the charCodeAt() method returns a Unicode character at a specified index.
 

Complete Program

 
chatAt_charCodeAt.ts
  1. class charAt_charCodeAt {  
  2.  charAt(str: string) {  
  3.   var position = str.charAt(0);  
  4.   var span = document.createElement("span");  
  5.   span.innerText = "charAt Method \n Display the first character of a string-> " + position + "\n";  
  6.   document.body.appendChild(span);  
  7.  }  
  8.  charCodeAt(str: string) {  
  9.   var unicode = str.charCodeAt(str.length - 1);  
  10.   var span = document.createElement("span");  
  11.   span.style.color = "green";  
  12.   span.innerText = "charCodeAt Method \n Display the unicode of the last character in a string-> " + unicode + "\n";  
  13.   document.body.appendChild(span);  
  14.  }  
  15. }  
  16. window.onload = () => {  
  17.  var obj = new charAt_charCodeAt();  
  18.  var str = prompt("Please enter a string");  
  19.  obj.charAt(str);  
  20.  obj.charCodeAt(str);  
  21. };  
chatAt_charCodeAt_MethodDemo.htm
  1. < !DOCTYPE html >  
  2.  <  
  3.  html lang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  meta charset = "utf-8" / >  
  9.  <  
  10.  title > TypeScript HTML App < /title>  
  11.  <  
  12.  link rel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  script src = "charAt_charCodeAt.js" > < /script>  
  17.  <  
  18.  /head>  
  19.  <  
  20.  body >  
  21.  <  
  22.  h3 > charAt() and charCodeAt() String Object Method In TypeScript < /h3>  
  23.  <  
  24.  div id = "content" / >  
  25.  <  
  26.  /body>  
  27.  <  
  28.  /html>  
chatAt_charCodeAt.js
  1. var charAt_charCodeAt = (function() {  
  2.  function charAt_charCodeAt() {}  
  3.  charAt_charCodeAt.prototype.charAt = function(str) {  
  4.   var position = str.charAt(0);  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "charAt Method \n Display the first character of a string-> " + position + "\n";  
  7.   document.body.appendChild(span);  
  8.  };  
  9.  charAt_charCodeAt.prototype.charCodeAt = function(str) {  
  10.   var unicode = str.charCodeAt(str.length - 1);  
  11.   var span = document.createElement("span");  
  12.   span.style.color = "green";  
  13.   span.innerText = "charCodeAt Method \n Display the unicode of the last character in a string-> " + unicode + "\n";  
  14.   document.body.appendChild(span);  
  15.  };  
  16.  return charAt_charCodeAt;  
  17. })();  
  18. window.onload = function() {  
  19.  var obj = new charAt_charCodeAt();  
  20.  var str = prompt("Please enter a string");  
  21.  obj.charAt(str);  
  22.  obj.charCodeAt(str);  
  23. };  
Output 1
 
Enter a string and click on "Ok":
 
enter-string.jpg
 
Output 2
 
result.jpg


Similar Articles