String Object Immutable Confusion

Reply

Join Date: Feb 2005
Posts: 46
Reputation: Gink is an unknown quantity at this point 
Solved Threads: 0
Gink Gink is offline Offline
Light Poster

Question about string editing

 
0
  #1
Feb 4th, 2005
Topic is misleading I edited it.

What would be the best way to put a period at the end of a sentence without just adding it to the last word or last variable etc.

Like say i had
String s = "Hello I am a sentence";

what would be the best way to add a period to the end?
Last edited by Gink; Feb 4th, 2005 at 6:42 pm. Reason: Googled
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: String Object Immutable Confusion

 
0
  #2
Feb 4th, 2005
s=s+".";
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: Gink is an unknown quantity at this point 
Solved Threads: 0
Gink Gink is offline Offline
Light Poster

Re: String Object Immutable Confusion

 
0
  #3
Feb 4th, 2005
lol, i meant the harder way I guess not the best. Is there a way of telling where a period should be by using some type of regular expression or string method?

Also, is this the best way for capitalizing the first Character in a string?

String s = "im a non capitalized sentence!";
char c;
c = s.charAt(0);
Character.toUpperCase(c)
s = s.replaceFirst(s.charAt(0),c)
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: Gink is an unknown quantity at this point 
Solved Threads: 0
Gink Gink is offline Offline
Light Poster

Re: String Object Immutable Confusion

 
0
  #4
Feb 4th, 2005
One more question, Say I have like 5 lines of text in a string
String s = "5 lines of text(pretend)"
and I want to reset this every time like s = ""
should I call System.gc() after I set s to another set of text(5 lines)?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: String Object Immutable Confusion

 
0
  #5
Feb 5th, 2005
no, there's no need.

String literals aren't affected by garbage collection, and anyway calling System.gc() isn't guaranteed to do anything

The only reason to use it is to hint the JVM that now would be a nice time to clear up memory before you start a resource intensive operation (either CPU or RAM), but otherwise it's pretty useless.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: String Object Immutable Confusion

 
0
  #6
Feb 5th, 2005
Originally Posted by Gink
lol, i meant the harder way I guess not the best. Is there a way of telling where a period should be by using some type of regular expression or string method?

Also, is this the best way for capitalizing the first Character in a string?

String s = "im a non capitalized sentence!";
char c;
c = s.charAt(0);
Character.toUpperCase(c)
s = s.replaceFirst(s.charAt(0),c)
Ah, you want a harder way?
  1. String theString = "Hello World";
  2. String theNewString = ((new StringBuffer(theString)).append(".")).toString();

A more efficient way to capitalise your first character would be to turn your String into a char array, capitalise element 0 in that, and then turn it back into a String.
Saves a few string operations.
  1. String theString = "hello world";
  2. char[] theArray = theString.toCharArray();
  3. theArray[0] = Character.toUpperCase(theArray[0]);
  4. String theOtherString = new String(theArray);
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC