Write Strings
Strings: String manipulation is the most common part of many java programs. Strings represent a sequence of characters....
https://www.computersprofessor.com/2017/04/write-strings.html?m=0
Strings:
|
||||
String
manipulation is the most common part of many java programs. Strings represent
a sequence of characters.
In java
strings are class objects and implemented using two classes namely string and
string buffer. A java string is an instantiated object of the string class. A
java string is not a character array and is not null terminated.
Strings may be
declared and created as follows.
Ex:
String first
name;
First name=new
string(“anil”);
These two
statements may be combined as follows:
String first
name=new string (“anil”);
Like arrays,
it is possible to get the length of string using the length method of the
string class
int m=first
name.length( );
note the use
of parentheses here java strings can be concatenated using the + operator.
Ex:
String
fullName=name1+name 2;
String
city1=”new”+”delhi”;
Whre name 1
and name 2 are java strings containing string constants. Another examples
System.out.println(first
name+”kumar”);
String arrays:
We can also
create and use arrays that contain strings . the statement
String
itemArray[ ]=new string[3];
Will create an
item array of size 3 to hold there string constants. We can assign the string
to the item array element by element using there different statements or more
efficiently using a for loop.
String methods:
The string
class defines a number of methods that allow us to accomplish a variety of
string manipulation tasks. Lists some of the most commonly used string
methods, and their tasks.
Some commonly
used string methods:
|