+ 1
How to delete something in the string?
i have string. for example my = "kitten" how can i delete "e" in that string... then my string will be my = "kittn" without make a new string...
5 Answers
+ 7
my = my[:4] + my[5:]
+ 14
https://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-JUMP_LINK__&&__python__&&__JUMP_LINK
Try concatenation with slicing.
my = my[:4] + my[-1:]
+ 8
public String missingChar(String str, int n) { String sub1= str.substring(0,n); String sub2= str.substring(n+1, str.length()); return sub1+sub2; }
or
public String missingChar(String str, int n) { StringBuffer buff= new StringBuffer(str); buff.delete(n,n+1); return buff.toString(); }
you can do that by converting string into character array
public class Practice { Ā
public static String missingChar(String s, int index) { return new StringBuilder(s).deleteCharAt(index).toString(); } Ā public static void main(String[] args) { assert "ktten".equals(missingChar("kitten", 1)); assert "itten".equals(missingChar("kitten", 0)); assert "kittn".equals(missingChar("kitten", 4)); } Ā }
+ 6
in python
import re
my1=''.join(re.findall("[^e]","kitten"))
print(my1)
+ 4
If you want to remove another letter or a string
replace ^e with ^(whatever you want to remove)