STRINGS
String unlike int, byte, and other primitive data types is
an object that represents sequence of char(a single character e.g
"A", " g" etc.) values. It is used to display texts (short
or long texts).
The official website of Java defines the String Class as the class that represents character strings. All string literals in Java programs, such as
The official website of Java defines the String Class as the class that represents character strings. All string literals in Java programs, such as
"abc"
, are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings
How to create String object
String can be created through various ways. Examples
include:
1. Create a String using a String literal
String literal is a string in between two double quotes.
String android1 = "rex";
2. Using the
"new" Keyword
String android2 = new String("android");
3. Concatenating (using the + operator and concat)
String android3 = "rex" + "android";
Or
String android3 = android + android2;
Or
String android3 = android1.concat(android2);
How to compare
Strings
They are different ways to compare String objects but I will
mention one
The equals() method:
equals() method compares two strings.
It compares the content of the strings. It will return true
if string matches, else returns false.
String android1 = "rex"; String android2 = "rex"; String android3 = "rexy"; android3.equals(android2);
It returns true
Android1.equals(android2);
It returns false
String Class Methods
They are many methods of the String Class. Here are some
methods:
1. charAt()
This method of String class returns the character located at
the specified index.
String android1 = "rexandroid"; TextView textView = (TextView) findViewById(R.id.textView); textView.setText(android1.charAt(7));
Text displays: d.
2. length()
This method of String class returns the number of characters
in a String.
TextView textView = (TextView) findViewById(R.id.textView); String android1 = "rexandroid.blogspot.com"; textView.setText(android1.length());
Text displays: 23
3. replace()
This method of String class replaces occurrences of
character with a specified new character.
String android2 = "rexdevelopers"; System.out.print(android2.replace(developers,android));
Text displays: rexandroid
4. startsWith()
This method of String class checks if this string starts
with given prefix and it returns either true or false.
String android3="rexandroid"; System.out.print(android3.startsWith("ex"));
Text displays: false
5. endsWith()
This method of String class checks if this string ends with
given suffix and it returns either true or false.
String android3 ="rexandroid"; System.out.print(android3.endsWith("oid"));
Text displays: true
6. substring()
This method of String class returns a part of the string.
substring() method has two forms.
public String substring(int begin);
It represents the starting point of the subtring. If the subString()
method is called with only one argument, the subString returned, will contain
characters from specified starting point to the end of the string.
TextView textView = (TextView) findViewById(R.id.textView); String android2 = "012345678910"; textView.setText(android2.substring(2));
Text displays: 2345678910
public String substring(int begin, int end);
If this method is called, the first argument specify the
starting point of subString and the second argument specify the end point of
substring.
TextView textView = (TextView) findViewById(R.id.textView); String android2 = "012345678910"; textView.setText(android2.substring(7,11));
Text displays: 78910
7. toLowerCase()
This method of String class returns string with all
uppercase characters converted to lowercase.
TextView textView = (TextView) findViewById(R.id.textView); String android2 = "ANDROID"; textView.setText(android2.toLowerCase());
Text displays: android
8. toUpperCase()
This method of String class returns string with all
lowercase character changed to uppercase.
TextView textView = (TextView) findViewById(R.id.textView); String android2 = "android"; textView.setText (android2.toUpperCase());
Text displays: ANDROID
9. trim()
This method returns a String and removes any whitespaces
before and after the String.
TextView textView = (TextView) findViewById(R.id.textView); String android2 = " rex android "; textView.setText(android2.trim());
Text displays: rexandroid
Read more about Strings: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
No comments:
Post a Comment