Strings - How To ?

Here are some good resources for finding out how to do something in Java:
Fred Swartz's Web-site  :  http://www.leepoint.net/notes-java/index.html   
Real's How-to-code : http://www.rgagnon.com/howto.html
Fluffy Cat (??!!)  http://www.fluffycat.com/java/  

Task needed

How to Code it
   str.   
represents a String variable

Count the number of letters in a String.

str.length( )

Get the first letter of a string.

str.substring(0,1)

Get the last letter of a string.

str.substring(str.length()-1,str.length())

Find out whether "FIS" appears somewhere in a String

if ( str.contains("FIS") )

Change ALL LETTERS in a String to capitals.

str = str.toUpperCase( );

Count the number of times the letter 'e' appears in a String

int count = 0;
for (int c=0; c < str.length(); c++)
{  if (str.charAt( c ) = = 'e' )
    {  count = count + 1; }
}

Remove the 3rd letter from a String

str = str.substring(0,3) + str.substring(4);

Remove all the 'e' characters from a String

int count = 0;
for (int c=0; c < str.length(); c++)
{  if (str.charAt( c ) = = 'e' )
    {
      str = str.substring(0,c) +
              str.substring(c+1) ; 
    }
}

Find the first blank space in a String, and then get the first WORD from the String

int  b =  str.indexOf( " ");

String fw = str.substring(0,b);

Search for a comma ',' in a name and exchange  the first and last names, like this:

      "Robinson, Jackie" -->  "Jackie Robinson"

int c = str.indexOf(",");

if (c >= 0)
{
    String last = str.substring(0,c);
   String  first = str.substring(c+1);
    str = first + " " + last;
}

Replace one letter with a different one.

find out how .replace works by reading http://mindprod.com/jgloss/gotchas.html#REPLACE

As you find other "how-to" solutions, it is useful to keep a list of notes in a notebook or on a USB stick.