String:
Strings, which are widely used in Java programming, are a sequence of
characters. In the Java programming language, strings are objects.
The Java platform provides the String class to create and manipulate
strings.
Creating Strings:
The most direct way to create a string is to write String greeting = "Hello world!";
Whenever it encounters a string literal in your code, the compiler
creates a String object with its value in this case, "Hello world!'.
public class StringDemo{
public static void main(String args[]){ char[]
helloArray = { 'h', 'e', 'l', 'l', 'o', '.'}; String helloString = new
String(helloArray); System.out.println( helloString );
}
}
String Length: Methods used to obtain information about an object are known as accessor methods. One accessor method that you
can use with strings is the length() method, which returns the number of
characters contained in the string object .
public class StringDemo {
public static void main(String args[]) { String
palindrome = "Dot saw I was Tod"; int len = palindrome.length();
System.out.println( "String Length is : " + len );
}
}
Concatenating Strings: The String class includes a method for concatenating two strings:
string1.concat(string2);
This returns a new string that is string1 with string2 added to it at
the end. You can also use the concat () method with string literals.
Some String Methods:
char charAt (int index): Returns the character at
the specified index. int compareTo(Object o): Compares this String to another
Object .
int compareTo(String anotherString): Compares two strings
lexicographically.
int compareToIgnoreCase(String str): Compares two strings
lexicographically, ignoring case differences.
String concat (String str): Concatenates the
specified string to the end of this string. boolean equals(Object anObject ):
Compares this string to the specified object .
int indexOf (int ch): Returns the index within this string of the first
occurrence of the specified character.
int length(): Returns the length of this string.
String replace(char oldChar, char newChar):Returns a new string
resulting from replacing all occurrences of oldChar in this string with
newChar.
String[] split (String regex): Splits this string around matches of the
given regular expression.
String toLowerCase(): Converts all of the characters in this String to
lower case using the rules of the default locale.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.