Strings. Part 2

Strings. Part 2


  1. Method endsWith()
  2. Splitting of the string to an array of integers. Method getBytes()
  3. Determination of the first occurrence of a character or string. Method indexOf()
  4. Checking, whether there is an empty string. Method isEmpty()
  5. The last occurrence of the character or string. Method lastIndexOf()
  6. Replacement of characters in the text. Method replace()
  7. Method startsWith()
  8. Selecting a substring from a string. Method substring()
  9. Splitting of string to the characters. Method toCharArray()
  10. Convert characters to uppercase and lowercase letters. Methods toLowerCase() and toUpperCase()
  11. Converting character array char[] into a String. Method copyValueOf()
  12. How to convert the number into its string representation? Method valueOf()

1. Method endsWith()

This method is intended to determine whether the substring is the end of original string.

The general view of method:

boolean endsWith(second_string)

Пример.

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

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

b = s1.endsWith("Ecl"); // b = false
b = s2.endsWith(s1);    // b = false
b = s1.endsWith(s2);    // b = true
b = s1.endsWith("Java"); // b = false
b = s1.endsWith("ipse"); // b = true

 

2. Splitting of the string to an array of integers. Method getBytes()

Method getBytes() allows you to get the value of the string as an array of type byte values in which each value is the character code in the symbol table.

For example. String

"Java"

consists of a sequence of characters with codes: 74 – ‘J’, 97 – ‘a’, 118 – ‘v’, 97 – ‘a’.

Example.

// method getBytes()
String s;
byte[] c;

s = "Java Eclipse";
c = s.getBytes(); // c = { 74, 97, 118, 97 } - character codes of word "Java"

 

3. Determination of the first occurrence of a character or string. Method indexOf()

Method indexOf() has several overloaded implementations.

In the first implementation, method returns the position of the first occurrence of the character with the specified code. In the second implementation, method returns the position of the first occurrence of the specified substring.

If no matches are found, the method returns -1.

The general form of the method implementations of indexOf():

int indexOf(character_code)
int indexOf(second_string)

An example of using indexOf() method.

// method indexOf()
String s;
int d;

s = "Java Eclipse";

// 1st variant of the method
d = s.indexOf(118); // d = 2, position of character with code 118 - 'v'
d = s.indexOf(130); // d = -1, the character with code 130 is not present in string s
d = s.indexOf(74); // d = 0

// 2nd version of the method - the position of the first occurrence of a string
d = s.indexOf("ava"); // d = 1
d = s.indexOf("abs"); // d = -1, no occurrences

 

4. Checking, whether there is an empty string. Method isEmpty()

Method isEmpty() returns true if the string is empty “”. Otherwise, method returns false. The general form of method:

boolean isEmpty()

Also, to check whether is the string empty, you can use the ‘==‘ operation.

Example.

// method isEmpty()
String s;
boolean b;

s = "Java Eclipse";
b = s.isEmpty(); // b = false

s = "";
b = s.isEmpty(); // b = true

// operation '=='
b = s==""; // b = true
s = "Java";
b = s==""; // b = false

 

5. The last occurrence of the character or string. Method lastIndexOf()

Method lastIndexOf() determines the position of the last occurrence of a character or substring in a given string. The general form of the two overloaded implementations of methods:

int lastIndexOf(character_code)
int lastIndexOf(second_string)

Example.

String s;
int d;

s = "Java";

d = s.lastIndexOf(97); // d = 3, the last occurrence of character with code 97 - 'a'
d = s.indexOf(97); // d = 1, first occurrence of a character with code 97

d = s.lastIndexOf("va"); // d = 2
d = s.lastIndexOf("a");  // d = 3

 

6. Replacement of characters in the text. Method replace()

Method replace() implements replacement of characters in the text. The method has several variants:

String replace(character1, character2)
String replace(string1, string2)
String replaceFirst(string1, string2)

In the methods above:

  • character1 – the character to be replaced;
  • character2 – character that replaces;
  • string1 – text that should be replaced;
  • string2 – text that replaces the other text.

replaceFirst() method replaces only the first occurrence of string2 in string1 (see. Example).

Example.

// method replace()
String s,s2;

s = "TextTextText";

// replacement of one character
s2 = s.replace('T', 'X'); // s2 = "XextXextXext"

// replacement of the text
s2 = s.replace("T", "TT"); // s2 = "TTextTTextTText"
s2 = s.replace("Text", "ABC"); // s2 = "ABCABCABC"

// method replaceFirst()
s2 = s.replaceFirst("Te", "ABC"); // s2 = "ABCxtTextText"

 

7. Method startsWith()

Method startsWith() determines whether the substring the beginning of the original string.

The substring is defined by the method parameter.

The general form of the method:

boolean startsWith(substring)

Example.

// method startsWith()
String s;
boolean b;

s = "TextTextText";

b = s.startsWith("Te"); // b = true
b = s.startsWith("ext"); // b = false

 

8. Selecting a substring from a string. Method substring()

Method substring() selects a substring from a string. The method has several implementations.

Variant 1.

String substring(index)

In this case, is selected the substring which starts from the position of the index to the end of the string.

Variant 2.

String substring(index1, index2)

In this case, is selected the substring starting from the position index1 and ending to the position index2.

Example.

// method substring()
String s, s2;

// variant 1
s = "This is a text";
s2 = s.substring(2); // s2 = "is is a text"

// variant 2
s2 = s.substring(2,3); // s2 = "i"
s2 = s.substring(2,4); // s2 = "is"
s2 = s.substring(6,13); // s2 = "s a tex"

 

9. Splitting of string to the characters. Method toCharArray()

Method toCharArray() converts a string in a character array char[].

Example.

// method toCharArray()
String s;
char c[];

s = "Java";
c = s.toCharArray(); // c = { 'J', 'a', 'v', 'a' }

 

10. Convert characters to uppercase and lowercase letters. Methods toLowerCase() and toUpperCase()

Method toLowerCase() converts the string to lowercase characters. Method toUpperCase() converts the string to uppercase characters.

The general view of methods:

String toLowerCase()
String toUpperCase()

Example.

// method toLowerCase()
String s1;
String s2;

s1 = "Java Eclipse";
s2 = s1.toLowerCase(); // s2 = "java eclipse"
s2 = s1.toUpperCase(); // s2 = "JAVA ECLIPSE"

 

11. Converting character array char[] into a String. Method copyValueOf()

Method copyValueOf() converts the array of char[] characters in the character string.

The general view of the method:

String copyValueOf(array_of_characters)

Example.

// method copyValueOf()
String s;
char[] c = { 'J', 'a', 'v', 'a' };

s = String.copyValueOf(c); // s = "Java"

 

12. How to convert the number into its string representation? Method valueOf()

Method valueOf() converts the numeric value or different type value in it’s string representation.

The method has several implementations which are different by parameters

String valueOf(boolean arg0)
String valueOf(char arg0)
String valueOf(char[] arg0)
String valueOf(double arg0)
String valueOf(float arg0)
String valueOf(int arg0)
String valueOf(long arg0)

Example.

// method valueOf()
String s;

// type 'double'
double d;
d = 3.8567;
s = String.valueOf(d); // s = "3.8567"

// type 'int'
int t;
t = -3903;
s = String.valueOf(t); // s = "-3903"

// type 'char'
char c = '+';
s = String.valueOf(c); // s = "+"

// type 'boolean'
boolean b;
b = true;
s = String.valueOf(b); // s = true