Strings. Part 1

Strings. Part 1


  1. How in Java implemented to work with character strings?
  2. How is described an object of type “character string”? Example of definition and use
  3. How to implement the initial initialization of the object (variable) of type String with its description? Example
  4. How to check two strings for equality? Method equals()
  5. How to determine the length of the string? Method length()
  6. How to determine the character at the specified index? Method charAt()
  7. Methods codePointAt() and codePointBefore()
  8. Methods compareTo() and compareToIgnoreCase()
  9. String concatenation. Method concat() and operation ‘+’
  10. Determining whether a substring in the string. Method contains()

1. How in Java implemented to work with character strings?

To represent data in Java, there are primitive data types. Among these types isnot string data type. Also there is no array of symbols.

In Java for work with strings is used class String. The object of the String class allows you to perform various operations on character strings.

Object (variable) of type String can be assigned to another object of type String.

Before use the object (variable) of type String must be initialized.

Character strings are the literals. In details, using the literals in Java is described here.

In Java, any string constant (string literal) is an object of class String.

Examples of string literal (string constants):

"This is a string of characters"
"Two\nstrings"
"Three\nstrings\n of characters"
"Topic: \'Characters strings\'"

 

2. How is described an object of type “character string”? Example of definition and use

Example the definition of objects (variables) of type String.

// definition of objects (variables) of type String
String s; // is described the object (variable) named s of type String
String str, myString;

After the description, you can assign the character string (literals) to object of type String:

s = "Hello world!"; 
str = s; // str = "Hello world!"
myString = "Hello " + "world!"; // myString = "Hello world!"

 

3. How to implement the initial initialization of the object (variable) of type String with its description? Example

The variable (object) of String type can be initialized during its description. Details about the use of variables in the programs is described here.

Example of variable initialization of type String.

// initialization of variables of type String
String s1 = "text";
String s2 = s1; // s2 = "text"
String s3 = "This is a " + s2, s4 = s3 + "!"; // s3 = "This is a text", s4 = "This is a text!"

 

4. How to check two strings for equality? Method equals()

In Java, the two strings are checked for equality:

  • by use the comparing operations ‘==‘ or ‘!=‘;
  • by use the equals() method of class String.

The general form of the method equals():

boolean equals(second_string)
boolean equalsIgnoreCase(second_string)

If strings are equal, then the equals() method returns true. Otherwise, it returns false.

equalsIgnoreCase() method works similarly to equals() method. This method differs from the method equals(), is that the upper-case characters are ignored when comparing strings. This means that the symbols of different registers are perceived as the same character.

Example 1.

// string comparison
boolean b;
String s1, s2;

s1 = "Text";
s2 = "Text";

// comparison operation ==
b = s1 == s2; // b = true
b = s1 == "TEXT"; // b = false

// method equals()
b = s1.equals(s2); // b = true
b = s1.equals("TEXT"); // b = false

Example 2. Use of conditional branch statement if.

// string comparison
String s1, s2;

s1 = "Text1";
s2 = "Text2";

// operation !=
if (s1!=s2)
    System.out.println("Strings are not equal ");
else
    System.out.println("Strings are equal ");

// method equals()
if (s1.equals(s2))
    System.out.println("Strings are equal ");
else
    System.out.println("Strings are not equal ");

Example 3. Use of method equalsIgnoreCase().

// method equalsIgnoreCase()
String s1, s2;
boolean b;

s1 = "Java Eclipse";
s2 = "JAVA ECLIPSE";

b = s1.equalsIgnoreCase("JAVA ECLIPSE"); // b = true
b = s2.equalsIgnoreCase(s1); // b = true

 

5. How to determine the length of the string? Method length()

To determine the length of the string (number of characters), it is necessary to use the method length() of class String.

General form of method length():

int length()

Example.

// method length()
String s;
s = "Method length()";

int n;
n = s.length(); // n = 15

 

6. How to determine the character at the specified index? Method charAt()

Method charAt() allows to get the character at the specified index.

char charAt(index)

Example of use method charAt().

// method charAt()
String s;

s = "Java Eclipse";

char c;
c = s.charAt(0); // c = 'J'
c = s.charAt(1); // c = 'a'
c = s.charAt(2); // c = 'v'
c = s.charAt(3); // c = 'a'

 

7. Methods codePointAt() and codePointBefore()

Method codePointAt() returns the character code by its index in the string. Method  codePointBefore() returns the character code, which comes before the specified index.

The general form of the methods:

int codePointAt(index)
int codePointBefore(index)

where index – position of the character in the string for which the code is returned.

Example.

// method codePointAt()
String s;

s = "Java Eclipse";

int n;
n = s.codePointAt(0); // n = 74 - code of character 'J'
n = s.codePointAt(5); // n = 69 - code of character 'E'

// method codePointBefore()
n = s.codePointBefore(3); // n = 118 - code of character 'v'
n = s.codePointBefore(1); // n = 74 - code of character 'J'

// error: "String index out of range: 0"
// n = s.codePointBefore(0);

 

8. Methods compareTo() and compareToIgnoreCase()

Methods compareTo() and compareToIgnoreCase() compare two strings in lexicographic order. Methods expedient for using in sorting algorithms rows.

The general form of the methods:

int compareTo(second_string)
int compareToIgnoreCase(second_string)

Methods return an integer value:

  • <0, if the second string follows after first in the lexicographical order;
  • 0, if they are equal;
  • >0, if the second string follows before the first string in the lexicographical order.

Method compareToIgnoreCase() is case insensitive.

Example.

// method compareTo()
String s1, s2;
int n;

s1 = "Java Eclipse";
s2 = "";

n = s1.compareTo(s2); // n = 12
n = s1.compareTo("Java"); // n = 8
n = s2.compareTo("T"); // n = -1

// method compareToIgnoreCase()
n = s1.compareToIgnoreCase("java ECLIPSE"); // n = 0, strings are equal

 

9. String concatenation. Method concat() and operation ‘+’

Method concat() allows to concatenate two strings together. Also, to concatenate string, you can use the overloaded operation ‘+‘.

The general form of the method:

String concat(second_string)

where second_string – string, which is added to the current string.

Example.

// method concat()
String s;

s = "Hello ";
s = s.concat("world"); // s = "Hello world"
s = s.concat("!");     // s = "Hello world!"

// operation '+'
s = "Hello ";
s = s + "world"; // s = "Hello world"
s = s + "!"// s = "Hello world!"

 

10. Determining whether a substring in the string. Method contains()

Method contains() is designed for determining the presence of the substring in the string.

The general form of the method contains():

boolean contains(second_string)

Method returns true, if there second_string is the substring of the given string. Otherwise, it returns false.

Example.

// method contains()
String s1, s2;
boolean b;

s1 = "Java Eclipse";
s2 = "Eclipse";
b = s1.contains(s2); // b = true
b = s2.contains("eclipse"); // b = false