How to create self string using special characters?
Creating self-string using special characters in JavaScript is a useful technique for adding unique and dynamic elements to your code. In this article, we will explore several methods for creating self-strings using special characters in JavaScript, including the use of escape characters, template literals, and the String.fromCharCode() method.
Escape Characters: One way to create self-string using special characters in JavaScript is by using escape characters. An escape character is a backslash () that is used to indicate that the character following it should be treated as a special character.
Example: To create a self-string that includes a newline, you would use the escape character followed by the letter “n”:
C++
#include <iostream>
int main() {
std::string selfString = "This is a self-string\nwith a newline." ;
std::cout << selfString << std::endl;
return 0;
}
|
Javascript
var selfString = "This is a self-string\nwith a newline." ;
console.log(selfString);
|
C#
using System;
public class GFG {
static public void Main()
{
string selfString
= "This is a self-string\nwith a newline." ;
Console.WriteLine(selfString);
}
}
|
Output
This is a self-string
with a newline.
Other common escape characters include “\r” for a carriage return, “\t” for a tab, and “”” for a double quote.
Template Literals: Another way to create self-string using special characters in JavaScript is by using template literals. Template literals are a new feature in JavaScript that allows you to create strings using template expressions. These expressions are enclosed in backticks ( ` `) rather than single or double quotes.
Example 1: To create a self-string that includes a variable, you would use template literals like this.
Javascript
var name = "John" ;
var selfString = `Hello, ${name}!`;
console.log(selfString);
|
Example 2: You can also use template literals to create self-string with special characters, like this:
Javascript
var selfString = `This is a self-string
with a newline.`;
console.log(selfString);
|
Output
This is a self-string
with a newline.
String.fromCharCode() Method: Another way to create self-string using special characters in JavaScript is by using the String.fromCharCode() method. This method is used to create a self-string from a series of Unicode character codes. For example, to create a self-string that includes a copyright symbol.
Example 1:
Javascript
var selfString = String.fromCharCode(169);
console.log(selfString);
|
Example 2: You can also pass multiple character codes to create multiple special characters at once,
Javascript
var selfString = String.fromCharCode(169, 174, 8482);
console.log(selfString);
|
Conclusion: In conclusion, creating self-string using special characters in JavaScript is a useful technique for adding unique and dynamic elements to your code. You can use escape characters, template literals, and the String.fromCharCode() method to create self-strings that include special characters like newlines, tabs, and special symbols. Practice with different combinations and possibilities to master this technique and enhance your coding skills.
Last Updated :
10 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...