Java String Handling Notes
String Handling in Java
String is a nothing but a text data. To deal with strings such as changing case, adding two strings or extracting part of the string finding length…etc. Java provides different types of classes and methods.
There are 3 classes to handle Strings in Java
String Handling String is a collection of characters that gives a specific meaning. String data will be given in quotation marks("). It is a one data type in Java.
Creating String Variable in Java
Example1:
String s1="book";
String Class in Java
To manage string data, this class provides several methods. The following methods are available in string class. These methods allow us to use them on strings to perform different types of operations on it. In this tutorial, you will learn how to use them for various purposes. Each and every function and its usage is explained in simple and straight forward with examples. Go a head and practice each and every example..
Table of contents
- Character array to String
- Byte array string
- Adding 2 strings
- Character Extraction:
- String to Array
- String Comparison:
- equals()
- equalsIgnoreCase()
- equals vs ==
- startsWith()
- endsWith()
- Searching Strings
- indexOf()
- lastIndexOf()
- Modifying a String
- substring()
- concat()
- replace()
- trim()
- Data conversion to String with
- valueOf()
- Changing the Case of the String
- String toLowerCase()
- String toUpperCase()
- String is immutable
Creating String Object
String s2= new String("book");
System.out.println(s1);
System.out.println(s2);
Convert Character array to String in Java
char ch[] = {'a','b','c'};
String s3 = new String(ch);
System.out.println(s3);
Byte array String in Java
byte ascii[] = {65,66,67,68,69,70};
String s4= new String(ascii);
System.out.println(s4);
Adding 2 strings in Java
String s1="Hello";
String s2="World";
String s3=s1+s2;
System.out.println(s1+" "+s2);
System.out.println(s3);
Character Extraction:
Extracts one character from the string at given position.
ch="abc".charAt(1);
o/p
b
String to Array in Java
toCharArray() : Converts String to Array
s1="Java";
char ch[]= s1.toCharArray();
ch[]={ 'J','a','v','a' };
String Comparison in Java:
There are 2 methods available to compares two string and return true if both are same
- equals()
- equalsIgnoreCase()
s1="Hello";
s2="hello";
s3="Good Day";
s4="Hello";
s1="HELLO";
s1.equals(S2); //not equal
s1.equals(S3); //not equal
s1.equals(s4); //equal
s1.equalsIgnoreCase(s4); //equal
Move to Menu
equals vs ==
.equals Compares two string values.
== compares references, it checks it is referring same string or not.
startsWith()
endsWith()
"coding-zon".endsWith("zon");
"coding-zon".startsWith("coding");
Searching Strings
- indexOf()
- lastIndexOf()
String s= "This is a demo of the indexof method.";
s.indexOf('t');
s.indexOf("the");
s.lastIndexOf("the");
s.indexOf('t',10);
Methods to modifying a String
- substring()
- concat()
- replace()
- trim()
substring():
Available is in two forms
- String substring(int startIndex);
- String substring(int startIndex,int endIndex );
s1="Hyderabad";
s1.substring(7);
s1.substring(7,3);
Concatenating two strings
To combine two string the given method is used.
concat()
s1="one"
s2="";
s2=s1.concat("two");
Replaces a char with a specified char within the string.
replace() Syntax: String replace(char original, char replacement); String s= "Hello".replace('l','w');
trim(); trim()--- removes trailing whitespaces in a string Syntax: String s =" Hello World "; s.length(); s.trim(); s.length(); o/p--->Hello Word
Data conversion to String with valueOf() method: This method converts a given datatype to string representation. Syntax: static String valueOf(double num); static String s1= String.valueOf(123.45); example:
Changing the Case of the Characters withing a String
String toLowerCase()
String toUpperCase()
example.
String s = "This is a text.";
String l = s.toLowerCase();
String u = s.toUpperCase();
String is immutable(can't be modifiable.). It means if the string is modified, a new string created with the modifications in another location in memory, and old string is destroyed in a memory.
s = "one";
System.out.println(s.concat("two"); //-->onetwo
System.out.println(s); // one
s=s.concat("Three"); //oneThree.
System.out.println(s);
To know more on String class visit: String class methods
StringBuffer Class in Java
StringBuffer class : peer class of String. This is growable, modifiable.
Methods
int length()
int capacity()
void setCharAt()
int append() StringBuffer reverse()
StringBuffer insert()
Examples: StringBuffer sb = new StringBuffer("Hello"); System.out.println(sb.length()); System.out.println(sb.capacity()); //default capasity: 26
setCharAt() StringBuffer sb = new StringBuffer("Hello"); sb.setCharAt(1,i); sb.setLength(2); System.out.println(sb); System.out.println(sb.charAt(5)); String Append append() adds one string to existing string StringBuffer sb = new StringBuffer("Hello"); String s = sb.append("word").toString(); System.out.println(s); String reverse reverse()
StringBuffer sb = new StringBuffer("Hello");
sb.reverse() //olleH
Inserting a new string in between the string.
insert()
StringBuffer sb = new StringBuffer("I java!");
sb.insert(2,"like") ;
To know more on StringBuffer class visit
StringBuilder class in Java
It is identical to StringBuffer. This class is synchronized. It means not threadsafe. Advantage with this class is - faster in performance.
example:
palindrome = madam, dad, mom, malayalam, tomato...etc.
Find a given string is palindrome or not
String str = new String("madam");
StringBuilder sb=new StringBuilder(str);
sb.reverse(); String rev=sb.toString(); if(str.equals(rev)){ System.out.println(:It is Palindrom"); }else{ System.out.println(:It is not a Palindrom"); }
Comments
Post a Comment