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

String Method

The document explains various string methods in programming, specifically focusing on the 'find', 'rfind', 'index', and 'count' methods. It illustrates how to locate substrings within a string and the differences between searching from the left and right. Examples are provided to demonstrate the usage and results of these methods, including handling invalid positions.

Uploaded by

AS
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)
12 views2 pages

String Method

The document explains various string methods in programming, specifically focusing on the 'find', 'rfind', 'index', and 'count' methods. It illustrates how to locate substrings within a string and the differences between searching from the left and right. Examples are provided to demonstrate the usage and results of these methods, including handling invalid positions.

Uploaded by

AS
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

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

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

s.count( )

• 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

s.index (‘ k ‘ ) substring not found

s.rindex( ‘o ‘ , 0 , 15 ) 16

s.count (‘me’) -0

s.count(‘how’) -1
fi
fi
ff
fi

You might also like