Results 1 to 8 of 8
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default Subtracting Strings

    Hi,

    I want to subtract a String from another. I tried the following which didn't work.
    Please advice solution.

    Java Code:
    		String s1 = "I am from UK.";
    		String s2 = "I am from";
    		
    		System.out.println(s1 - s2);
    Thanks.

  2. #2
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    you could use the substring method of the String class.

    Java Code:
    String s1 = "I am from UK.";
    String s2 = s1.substring(0, 9);
    
    System.out.println(s2);
    As i learned from CaptainMorgan, the first argument of the substring method operates on a 0 based index, meaning that the index first character of the string is 0. and then, for the second argument (9) operates on a 1 based index, meaning that the index of the first character is 1.

  3. #3
    rpwtdj's Avatar
    rpwtdj is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

    Default

    String s1 = "I am from UK.";
    String s2 = "I am from";
    System.out.println(s1.substring(0,s2.length()));

  4. #4
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Thanks all of you.
    But it wont work if am not sure about the indexes.

    Java Code:
    String s1 = "I am from UK.";
    String s2 = "I am from";
    System.out.println(s1 - s2); // output should be -- UK
    
    String s3 = "I am from UK.";
    String s4 = "UK";
    System.out.println(s3 - s4); // output should be -- I am from
    I want some generic way of doing it if possible.
    Thanks for your time.

  5. #5
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Regex replace .. Replace (-2nd param with empty string) ..
    dont worry newbie, we got you covered.

  6. #6
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Thanks everyone. Following worked for me.

    Java Code:
    String s1 = "I am from UK.";
    String s2 = "I am from";
    System.out.println(s1.replaceAll(s2," "));

  7. #7
    him4all is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default Subtracting Strings

    String s1 = "I am from UK.";
    String s2 = "I am from";
    System.out.println(s1.substring(s2.length(),s1.len gth()));
    Last edited by him4all; 10-08-2009 at 03:22 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by him4all View Post
    String s1 = "I am from UK.";
    String s2 = "I am from";
    System.out.println(s1.substring(s2.length(),s1.len gth()));
    I have to admit to not liking this solution at all as

    1) it doesn't really remove a subString based on its match to another string.
    2) it's worse than solutions already provided in this old old thread.

    I would suggest that you withdraw it.

Similar Threads

  1. Comparison of Strings
    By Cero.Uno in forum New To Java
    Replies: 3
    Last Post: 02-11-2008, 02:46 AM
  2. Help with drawing strings!
    By JavaInLove in forum AWT / Swing
    Replies: 1
    Last Post: 02-05-2008, 03:39 AM
  3. Help with Vectors and Strings...
    By kaban in forum New To Java
    Replies: 2
    Last Post: 12-09-2007, 09:04 AM
  4. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 AM
  5. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •