| | |
String Object Immutable Confusion
![]() |
•
•
Join Date: Feb 2005
Posts: 46
Reputation:
Solved Threads: 0
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?
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
•
•
Join Date: Feb 2005
Posts: 46
Reputation:
Solved Threads: 0
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)
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)
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.
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.
•
•
•
•
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)
Java Syntax (Toggle Plain Text)
String theString = "Hello World"; 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)
String theString = "hello world"; char[] theArray = theString.toCharArray(); theArray[0] = Character.toUpperCase(theArray[0]); String theOtherString = new String(theArray);
![]() |
Similar Threads
- convert string into object (Visual Basic 4 / 5 / 6)
- String to Object (Java)
- Size of string object??? (C++)
- making a string .. name of an object (Java)
Other Threads in the Java Forum
- Previous Thread: Help!! I'm Drowning
- Next Thread: Axis Labels
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide image int j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux mac main map method mobile myregfun netbeans nonstatic notdisplaying number online pearl printf problem program project qt researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






