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
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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:
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:
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).
nhiap6 (November 16th, 2012)
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