0% found this document useful (0 votes)
31 views2 pages

String Method

String methods allow you to perform operations on string objects. Some key string methods include: 1. s.indexOf(substring) and s.lastIndexOf(substring) which return the index of the first or last occurrence of a substring within the string. 2. s.substring(start, end) which returns a subset of the string between the start and end indexes. 3. s.count(substring) which returns the number of occurrences of a substring in the string. These string methods allow you to search, extract, and analyze portions of strings stored in variables.

Uploaded by

Abhinav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

String Method

String methods allow you to perform operations on string objects. Some key string methods include: 1. s.indexOf(substring) and s.lastIndexOf(substring) which return the index of the first or last occurrence of a substring within the string. 2. s.substring(start, end) which returns a subset of the string between the start and end indexes. 3. s.count(substring) which returns the number of occurrences of a substring in the string. These string methods allow you to search, extract, and analyze portions of strings stored in variables.

Uploaded by

Abhinav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

String Method

• Methods are the member of the class which performs operation upon the data of an object

S = “ hello , how are you “

This is how String is stored

• It has 18 characters and it is hold by a reference that is s

s. nd ( sub [ , start [ ,end]])

• sub ( nd the occurrence of the substring )


• Methods are called by using object name that is variable name
s. nd(‘o’)
It will start looking for o from the left hand of the string . The result is 4 cause we found ‘ o ‘ there
s. nd (‘ how ‘)
The result will be 7
s. nd(‘ k ‘)
The result will be -1 . Why it is -1 because the character will start from 0 . -1 means outside the
range . So it is invalid position .

s. nd ( ‘ o ‘ , 5 )

sub start

• If you want to nd out after the 5th index we write 5 in starting index
• If you want to nd out other substring then you should give starting and ending index
s . nd (‘o’ , 5 , 7 )
It will check from 5 to 7
• You can pass the nd by single , double or 3 parameters

s.r nd ( sub [ , start [ ,end]])

• It is same as nd but in nd we where searching from left but in r nd will search substring from
right
s.r nd(‘o’) 16 index

s. nd (‘o ‘ , 0 , 15 ) 8 index

• In r nd ending index will work but in nd starting index will work


• -1 will return if its out of string

[Link] ( )
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
• [Link] and s. nd is same but have minor di erences . rindex is same as r nd

[Link]( )

• It will count the number of occurrence


Lets take an example - character ‘ o ‘

‘o’ it repeated 3 times

• The count gives counting of the string. It will not gives all the indexes . It will only count

s. nd ( ‘ k ‘ ) -1

[Link] (‘ k ‘ ) substring not found

[Link]( ‘o ‘ , 0 , 15 ) 16

[Link] (‘me’) -0

[Link](‘how’) -1
fi
fi
ff
fi

You might also like