Home » Tech Tutorials » Web Development » JavaScript » JavaScript String Methods

JavaScript String Methods

JavaScript String Methods

JavaScript లో text data ను String అంటారు. ఉదాహరణకు పేరు, email address, message, product title వంటి values strings గా ఉంటాయి.

String methods ఉపయోగించి text ను మార్చవచ్చు, వెతకవచ్చు, కొంత భాగాన్ని తీసుకోవచ్చు మరియు extra spaces ను తొలగించవచ్చు. String methods సాధారణంగా original string ను మార్చకుండా కొత్త string లేదా result ను return చేస్తాయి.

1. length

length property string లో ఎన్ని characters ఉన్నాయో చెబుతుంది. ఇది method కాదు; అందువల్ల length తర్వాత parentheses ఉపయోగించము.

2. toUpperCase() మరియు toLowerCase()

toUpperCase() string ను capital letters గా మారుస్తుంది. toLowerCase() string ను small letters గా మారుస్తుంది.

3. trim()

trim() string ప్రారంభం మరియు చివరలో ఉన్న extra spaces ను తొలగిస్తుంది. User input ను process చేసేటప్పుడు ఇది చాలా ఉపయోగకరం.

4. includes()

includes() ఒక string లో మరొక text ఉందా అని తనిఖీ చేస్తుంది. Result గా true లేదా false వస్తుంది. ఇది case-sensitive; అంటే JavaScript మరియు javascript ఒకేలా పరిగణించబడవు.

5. startsWith() మరియు endsWith()

startsWith() string ఒక నిర్దిష్ట text తో ప్రారంభమవుతుందా అని చెబుతుంది. endsWith() string ఒక నిర్దిష్ట text తో ముగుస్తుందా అని చెబుతుంది.

6. slice()

slice() string లోని ఒక భాగాన్ని తీసుకోవడానికి ఉపయోగిస్తాము. మొదటి index నుండి రెండవ index కి ముందు వరకు characters తీసుకుంటుంది. Index counting zero నుండి ప్రారంభమవుతుంది.

7. replace()

replace() string లోని ఒక text ను మరొక text తో మార్చుతుంది. సాధారణంగా ఇది మొదట కనిపించే matching text ను మాత్రమే మార్చుతుంది.

String methods ను chain కూడా చేయవచ్చు. ఉదాహరణకు trim() తర్వాత toUpperCase() ఉపయోగించవచ్చు. ఒక method ఇచ్చిన result పై తరువాతి method పని చేస్తుంది.

Simple Explanation

String methods అంటే text పై పనులు చేయడానికి ఉపయోగించే ready-made JavaScript tools. ఒక sentence లో spaces తొలగించడం, letters ను uppercase చేయడం, ఒక word ఉందో లేదో చూడటం వంటి పనులను ఇవి సులభం చేస్తాయి.

ఉదాహరణకు user పేరు ” ravi “ గా వచ్చినప్పుడు trim() తో spaces తొలగించి, toUpperCase() తో “RAVI” గా మార్చవచ్చు.

Real-Life Example

ఒక online shopping website లో product search box ఉందని అనుకుందాం. User ” laptop “ అని type చేస్తే, మొదట trim() తో extra spaces తొలగించవచ్చు. తరువాత toLowerCase() ఉపయోగించి search text ను consistent format లోకి మార్చవచ్చు. includes() తో product name లో user వెతికిన word ఉందో లేదో తనిఖీ చేయవచ్చు.

Code Examples

String ను clean చేసి uppercase గా మార్చడం

const message = "  JavaScript Strings  ";
const cleanedMessage = message.trim();
const upperMessage = cleanedMessage.toUpperCase();

console.log(upperMessage);

మొదట trim() ప్రారంభం మరియు చివరలోని spaces ను తొలగిస్తుంది. తరువాత toUpperCase() మిగిలిన text ను capital letters గా మార్చుతుంది.

JAVASCRIPT STRINGS

String లో text ఉందో లేదో తనిఖీ చేయడం

const email = "student@example.com";

console.log(email.includes("@"));
console.log(email.startsWith("student"));
console.log(email.endsWith(".com"));

ఈ code email లో @ symbol ఉందా, student తో ప్రారంభమవుతుందా, .com తో ముగుస్తుందా అని తనిఖీ చేస్తుంది. ప్రతి method boolean result ను ఇస్తుంది.

true
true
true

slice() మరియు replace() ఉపయోగించడం

const course = "JavaScript Basics";
const shortName = course.slice(0, 10);
const updatedCourse = course.replace("Basics", "Methods");

console.log(shortName);
console.log(updatedCourse);

slice(0, 10) index 0 నుండి index 10 కి ముందు వరకు text తీసుకుంటుంది. replace() Basics అనే word ను Methods తో మార్చుతుంది.

JavaScript
JavaScript Methods

Line-by-Line Explanation

const message = ” JavaScript Strings “;

message అనే constant variable లో ప్రారంభం మరియు చివర extra spaces ఉన్న string ను store చేస్తున్నాము.

const cleanedMessage = message.trim();

trim() message string యొక్క రెండు వైపులా ఉన్న extra spaces ను తొలగించి, కొత్త string ను cleanedMessage లో store చేస్తుంది.

const upperMessage = cleanedMessage.toUpperCase();

cleanedMessage పై toUpperCase() method ఉపయోగించి అన్ని letters ను uppercase గా మార్చి upperMessage లో store చేస్తున్నాము.

console.log(upperMessage);

upperMessage లో ఉన్న final text ను console లో చూపిస్తుంది.

Try It

Try this JavaScript code:

Example

const userName = "  Anu  ";
const cleanedName = userName.trim();
const displayName = cleanedName.toUpperCase();

console.log(displayName);

Try It Yourself »

Practice

Practice:

పై code ను మార్చి productName లోని extra spaces ను తొలగించండి. తరువాత product name ను uppercase గా మార్చి మొదటి output గా చూపించండి. చివరగా ఆ cleaned product name లో “SCRIPT” text ఉందో లేదో includes() తో తనిఖీ చేయండి.

Hint:

మొదట trim() ఉపయోగించండి. తరువాత వచ్చిన result పై toUpperCase() ఉపయోగించవచ్చు. includes() method true లేదా false ను return చేస్తుంది.

Common Mistakes

length ను method లాగా length() అని రాయడం.

length ఒక property కాబట్టి parentheses లేకుండా text.length అని రాయాలి.

trim() original string ను permanently మార్చుతుందని అనుకోవడం.

String methods సాధారణంగా కొత్త result ను return చేస్తాయి. ఆ result ను variable లో store చేయాలి లేదా ఉపయోగించాలి.

includes() case-sensitive అని గుర్తించకపోవడం. ఉదాహరణకు “JavaScript” లో “javascript” ఉందని తనిఖీ చేయడం.

అవసరమైతే రెండింటినీ ఒకే case లోకి మార్చి includes() ఉపయోగించండి. ఉదాహరణకు toLowerCase() తర్వాత search చేయవచ్చు.

slice() లో చివరి index కూడా include అవుతుందని అనుకోవడం.

slice(start, end) లో start index include అవుతుంది, కానీ end index include కాదు.

replace() ఉపయోగించిన తరువాత result ను variable లో store చేయకపోవడం.

replace() ఇచ్చే కొత్త string ను variable లో store చేయండి లేదా వెంటనే console.log() లో ఉపయోగించండి.

Interview Questions

JavaScript లో string యొక్క length ఎలా తెలుసుకుంటారు?

Answer:

String తర్వాత length property ఉపయోగించాలి. ఉదాహరణకు const count = text.length;. length ఒక property కాబట్టి parentheses అవసరం లేదు.

trim() method యొక్క ఉపయోగం ఏమిటి?

Answer:

trim() string ప్రారంభం మరియు చివరలో ఉన్న whitespace ను తొలగించి కొత్త string ను return చేస్తుంది. Form లేదా user input clean చేయడానికి ఇది ఉపయోగకరం.

includes() method ఏ value ను return చేస్తుంది?

Answer:

includes() search చేసిన text string లో ఉంటే true, లేకపోతే false ను return చేస్తుంది. ఇది case-sensitive.

slice() మరియు replace() మధ్య తేడా ఏమిటి?

Answer:

slice() string లోని ఒక భాగాన్ని తీసుకుంటుంది. replace() string లో matching text ను వేరే text తో మార్చుతుంది. రెండూ సాధారణంగా కొత్త string ను return చేస్తాయి.

Exam Questions

క్రింది code output ఏమిటి? const text = “Hello World”; console.log(text.toLowerCase());

Answer:

Output: hello world

ఒక string లో “@” ఉందో లేదో తనిఖీ చేయడానికి ఏ method ఉపయోగిస్తారు?

Answer:

includes() method ఉపయోగిస్తారు. ఉదాహరణకు email.includes(“@”);

const word = “JavaScript”; లో word.slice(0, 4) output ఏమిటి?

Answer:

Output: Java. ఎందుకంటే index 0 నుండి index 4 కి ముందు వరకు characters తీసుకోబడతాయి.

String methods ఉపయోగించినప్పుడు original string మారుతుందా?

Answer:

సాధారణంగా కాదు. String immutable కాబట్టి methods కొత్త string లేదా result ను return చేస్తాయి. ఆ result ను అవసరమైతే కొత్త variable లో store చేయాలి.

Quick Revision

  • length string లోని characters count ను ఇస్తుంది.
  • toUpperCase() letters ను uppercase గా మారుస్తుంది.
  • toLowerCase() letters ను lowercase గా మారుస్తుంది.
  • trim() ప్రారంభం మరియు చివర extra spaces ను తొలగిస్తుంది.
  • includes() text ఉందో లేదో true లేదా false గా చెబుతుంది.
  • startsWith() మరియు endsWith() string ప్రారంభం లేదా ముగింపును తనిఖీ చేస్తాయి.
  • slice() string లోని ఒక భాగాన్ని తీసుకుంటుంది.
  • replace() matching text ను మరో text తో మార్చుతుంది.
  • String methods సాధారణంగా original string ను మార్చకుండా కొత్త result ను return చేస్తాయి.
  • includes(), startsWith() మరియు endsWith() case-sensitive methods.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top