943,929 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5859
  • Java RSS
Feb 4th, 2005
0

Question about string editing

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Gink is offline Offline
46 posts
since Feb 2005
Feb 4th, 2005
0

Re: String Object Immutable Confusion

s=s+".";
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Feb 4th, 2005
0

Re: String Object Immutable Confusion

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)
Reputation Points: 10
Solved Threads: 0
Light Poster
Gink is offline Offline
46 posts
since Feb 2005
Feb 4th, 2005
0

Re: String Object Immutable Confusion

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)?
Reputation Points: 10
Solved Threads: 0
Light Poster
Gink is offline Offline
46 posts
since Feb 2005
Feb 5th, 2005
0

Re: String Object Immutable Confusion

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Feb 5th, 2005
0

Re: String Object Immutable Confusion

Quote 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?
Java Syntax (Toggle Plain Text)
  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.
Java Syntax (Toggle Plain Text)
  1. String theString = "hello world";
  2. char[] theArray = theString.toCharArray();
  3. theArray[0] = Character.toUpperCase(theArray[0]);
  4. String theOtherString = new String(theArray);
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help!! I'm Drowning
Next Thread in Java Forum Timeline: Axis Labels





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC