DEV Community

S Sarumathi
S Sarumathi

Posted on

String Program

1. What is String?

A String is a sequence of characters.

In simple words:
String = collection of letters, numbers, or symbols inside quotes

Examples of String

  • "Hello"

  • "Java"

  • "12345"

  • "Chennai"

  • "Hello@123"

All of these are Strings because they are inside double quotes.
Program:

const str="priya"
console.log(str[0]);
Enter fullscreen mode Exit fullscreen mode

Output:

2.

const str="priya"
console.log(str[str.length-1]);
Enter fullscreen mode Exit fullscreen mode

Output:

3.

const str="priya"
console.log(str[Math.floor(str.length/2)]);
Enter fullscreen mode Exit fullscreen mode

Output:

4.

const str="priya"
console.log(str.toUpperCase());
Enter fullscreen mode Exit fullscreen mode

Output:

5.

const str="priya"
console.log(str.toLowerCase());

Enter fullscreen mode Exit fullscreen mode

Output:

6.

let str="priya"
str=str.toUpperCase()
console.log(str.toUpperCase());
console.log(str.toLowerCase());
console.log(str);
Enter fullscreen mode Exit fullscreen mode

Output:

7.

let name = "saha";

let result = name[0].toUpperCase() + 
             name.slice(1, -1) + 
             name[name.length - 1].toUpperCase();

console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output:

8.

let name = "saha";

let mid = Math.floor(name.length / 2);

let result = name.slice(0, mid) + 
             name[mid].toUpperCase() + 
             name.slice(mid + 1);

console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output:

Top comments (0)