



|
|
CS1 Course Site
|
Wait , WAIT !!What's that word ? ( Class Notes ( without definitions ) )
Monday November 6 , 2000
|
|
String Processing
specification (very partial) of java.lang.String class.
- < string > .length () -- > < int >
``CS1 exams are fun! '' .length = 18
- < string > .indexOf (< string >) -- > < int >
``frim fram sauce''.indexOf ( ``fr'') = 0
``frim fram sauce''.indexOf ( ``fra'') = 5
- < string > .substring (< int >) -- > < string >
returns the substring from the position specified by the parameter to the end of the string.
``Monday''.substring (3) = ``day''}
- < string > .substring (< int >, < int >) -- > < string >
returns the substring from the position of the first parameter to the position one less than the second parameter.
``tired hand''.substring (2, 7) = ``red h''
``Holliday, Billy'' singer.indexOf ( ``, '' )
static private string firstName (string dsname)
{
< compute portion of comma >;
< compute first name >;
return the first name;
}
static private void computeTheAverage ()
{
int sum = 0;
for (int x = 0; x < nrElements; x + +)
{
sum = sum + a [x];
}
average = (double) sum / (double) nrElements;
}
static private void displayTheNumberAboveAverage ()
{
for (int x = 0; x < nrElements; x = x + 1)
{
if (a [x] > average)
{
IO.println (a [x]);
};
};
};
More String Functionality...
- < string > .equals (< string >) -- > < boolean >
``Cat''.equals ( ``dog'') = false
``cat''.equals ( ``cat'') = true
``cat''.equals ( ``Cat'') = False
- < string > .equalsIgnoreCase (< string >) -- > < boolean >
- < string > .compareTo (< string >) -- > < int >
Returns...
- 0 - if the two strings are equal
- Something negative if the recieving string is alphabetically less than the parameter.
- Something positive if the recieving string is alphabetically greater than the parameter.
Ex
``cat''.compareTo ( ``bat'') -- > +
``bat''.compareTo ( ``bats'') -- > -
|
|