Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-09-2008, 04:15 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Subtracting Strings
Hi,

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

Code:
String s1 = "I am from UK."; String s2 = "I am from"; System.out.println(s1 - s2);
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-10-2008, 12:02 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
you could use the substring method of the String class.

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.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-10-2008, 04:56 AM
rpwtdj's Avatar
Member
 
Join Date: Dec 2007
Posts: 15
rpwtdj is on a distinguished road
Send a message via MSN to rpwtdj
String s1 = "I am from UK.";
String s2 = "I am from";
System.out.println(s1.substring(0,s2.length()));
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-10-2008, 11:58 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Thanks all of you.
But it wont work if am not sure about the indexes.

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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-10-2008, 12:46 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
Regex replace .. Replace (-2nd param with empty string) ..
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-10-2008, 05:19 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Thanks everyone. Following worked for me.

Code:
String s1 = "I am from UK."; String s2 = "I am from"; System.out.println(s1.replaceAll(s2," "));
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of Strings Cero.Uno New To Java 3 02-11-2008 04:46 AM
Help with drawing strings! JavaInLove AWT / Swing 1 02-05-2008 05:39 AM
Help with Vectors and Strings... kaban New To Java 2 12-09-2007 11:04 AM
Comparing Strings Java Tip Java Tips 0 12-03-2007 11:44 AM
reversing Strings Java Tip Java Tips 0 11-11-2007 10:24 PM


All times are GMT +3. The time now is 02:01 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org