String are most widely used in any programming language. String is basically sequence of characters and. in Java Strings are treated as objects.
1. String Literal:
Syntax: String name= āString you wantedā;
Sample Code: String str= āNihal Kurreā;
2. Using new KeyWord:
Syntax: String name=new String( āString you wantedā);
Sample Code: String str= new String( āNihal Kurreā);
Note: Strings are immutable [It means once if they are declared they cannot be changed]
What can you do with Strings??
You can almost do anything design algorithms, manipulate, convert into objects etc.
Java String class provides a lot of methods to perform and manipulate such as :
Equals(), slpit(), length(), compareTO(), substring(), concate(), indexed(), repace(), charAt().. etc.
No:
|
Method
|
Description
|
|
1
|
length()
|
Returns length of a given string
|
|
2
|
charAt(int index)
|
Returns char value of index
|
|
3
|
Boolean contains()
|
Returns true/false
|
|
4
|
String substring(int begin index)
|
Returns substring for given index
|
|
5
|
String substring(1stIndex, Last index)
|
Returns substring for given begin index to end index
|
|
6
|
String substring(1stIndex)
|
Return the substring value
|
|
7
|
Boolean isEmpty()
|
Returns true/false
|
|
8
|
Equals(Object another)
|
Compares 2 objects
|
|
9
|
String replace(charSeq old, CharseqNew)
|
Replace your old char char with given new char
|
|
10
|
Int indexOf(int char)
|
Returns index of char value
|
|
11
|
int indexOf(int ch, int fromIndex)
|
returns specified char value index starting with given index
|
|
12
|
String toUpperCase()
|
Convers string to upper case
|
|
13
|
String toLowerCase()
|
Convers string to lower case
|
|
14
|
String trim()
|
Removes beginning and ending spaces in the given string
|
|
15
|
String valueOf(int value)
|
Convert given type into string and it is overloaded
|
|
Did you Know??
o How to create an immutable class?
o What is string constant pool?
o What code is written by the compiler if you concat any string by + (string concatenation operator)?
o What is the difference between StringBuffer and StringBuilder class?
No comments:
Post a Comment