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 8 of 8

Thread: replacement substring

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default replacement substring

    Hi there all, just trying to create a method to replace a old substring with a new one - i have denoted these by putting Source(source string), old(old substring string), new(new substring to replace old).
    public static String[][] ftSwapSource =         
       { 
          {"ft old",       "File Transfered new" } 
     };
    public static String replaceEach(String ftSwapSource, String oldString, String newString)

    the oldString should be replaced by newString ("ft old" gets replaced by "File Transdered new").

    Any ideas?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: replacement substring

    I'm not entirely clear what you're actually trying to do, but you should probably check out the API for String. Hint- You're trying to replace all of the substrings that match a particular pattern, right?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: replacement substring

    yeh using replaceAll() to replace the old version with the new version from the source string.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: replacement substring

    ...does that mean you solved your problem? I'm not really sure what you're asking, or whether you're asking anything.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: replacement substring

    lol - no I'm still asking.

    This method needs to replace the old substring with the new one;
    public static String replaceEach(String source, String oldString, String newString)
    {
    method to replace oldString with newString from the source String
    }
    --------------------------------------------------
    public static String[][] source =
    {
     
    {"ft old", "File Transfered new" }
     
    };

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: replacement substring

    Okay, and did you check out the API for String? I gave you a pretty big hint.

    And I'll even give you a link to the API: Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: replacement substring

    still can't seem to get the method to work - can you see anything wrong?

      public static replace String replaceEach(sourceString, oldString, newString)
      {
          Stringbuilder sourceString = new StringBuilder("result");
          for (int i = 1; i <= 100, i ++)
          {
             sourceString.replaceAll(oldString, newString);
          }
          return String result;
       }

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: replacement substring

    Strings are immutable and cannot be modified once instantiated, which is why the replaceAll() function returns the resulting string. So you need to get the resulting string from calling the replaceAll function before continuing the loop

Similar Threads

  1. String replacement
    By codethrupain in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2011, 10:10 PM
  2. SUBSTRING
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 1
    Last Post: November 12th, 2010, 02:57 AM
  3. Substring help
    By Roach in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2010, 07:57 AM
  4. Page Replacement Algorithms
    By smokeyjoey in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 7th, 2009, 09:38 PM
  5. Substring function
    By bristol580 in forum Java SE APIs
    Replies: 2
    Last Post: November 12th, 2009, 10:29 AM