So I have same basic String like s = "Michael", and what i'm trying to do is when some name is entered like michael that last 2 characters delete "el" and new chars like "em" gets added to the entered word so the result is "Michaem" ? thanks
Printable View
So I have same basic String like s = "Michael", and what i'm trying to do is when some name is entered like michael that last 2 characters delete "el" and new chars like "em" gets added to the entered word so the result is "Michaem" ? thanks
Are you intend to delete the last 2 characters that contain only "el" ?? , or you want to delete every last 2 characters and replace them with "em" ?
if you want to replace every last 2 characters with "em", then you can do the following:
Code :public class StringReplacement { public static void main(String[] args) { if (args.length < 1) { System.err.println("Usage: java StringReplacement [string]\ne.g.: java StringReplacement Michael"); System.exit(-1); } str = args[0].trim(); str = str.replace(str.subSequence(str.length() - 2, str.length()), "em"); System.out.println(str); } }
If you're going to delete only last 2 characters that matched 'el', then here's the code:
Code :str = str.replaceAll("el$", "em"); System.out.println(str);
Anyway, REGEX is the most efficient way to handle string manipulation (just like what i did with the last example above).
that helped alot but I kinda changed it cause I dont understand that of statements.But the main thing works. Thanks . btw : here's my code so if you could comment -- [Java] import java.util.Scanner; public class nino1 { public static void main - Pastebin.com