+ 3
How to remove a desired part of string? (not using indexes)
3 ответов
+ 3
If you mean you for example want to subtract " world" from "Hello world!" so that you get "Hello!", you can use the replace method:
Replacing " world" with an empty string:
"Hello world!".replace(" world", "")
---> "Hello!"
Replace method will by default replace all occurences of " world" with the empty string. You can prevent this by adding third argument, which represents how many occurences of " world" you want to replace.
Replacing 2 occurences of " world" with an empty string:
"Hello world world world!".replace(" world", "", 2)
---> "Hello world!"
+ 7
Jeet Swarnakar , what do you mean by saying 'subtract a part of a string'? It would be great if you could do a sample for us. Thanks.
+ 2
thanks Seb TheS....you got what i meant