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