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.

Results 1 to 3 of 3

Thread: string manipulation

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default string manipulation

    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


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: string manipulation

    Quote Originally Posted by nhiap6 View Post
    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).

  3. The Following User Says Thank You to yauritux For This Useful Post:

    nhiap6 (November 16th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: string manipulation

    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

Similar Threads

  1. string manipulation
    By nhiap6 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 16th, 2012, 12:28 PM
  2. [SOLVED] problem with time manipulation!
    By arvindbis in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 30th, 2011, 02:47 PM
  3. String Manipulation.
    By Nemus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2011, 03:02 PM
  4. question on list manipulation
    By andrefaelnar in forum Java Theory & Questions
    Replies: 11
    Last Post: August 18th, 2010, 07:30 PM
  5. String manipulation help
    By Duff in forum Loops & Control Statements
    Replies: 7
    Last Post: March 19th, 2010, 04:02 AM