Learning Methods in Java String Class - 1

in #utopian-io7 years ago (edited)

What Will I Learn?

  • You will learn Every detail about the Java String Class
  • You will learn how to use the following methods in Java String Class;
  • codePointAt();
  • codePointBefore();
  • codePointCount();
  • startsWith();

Requirements

  • NetBeans or Eclipse IDE
  • Basic Knowledge of Java

Difficulty

  • Intermediate

Tutorial Contents

String Class
A string is a sequence of alphanumeric characters, these could form sentences from words or be inform of a code. Strings in java, when compared to other languages such as C, C# and C++ is different this is because it is an object type of the string class. This causes any manipulation made on strings to be possible through methods from the string class.
Basically, once a string is created, the characters which have made up the string cannot be changed or altered. Any alterations to these characters would be stored in a new string object.

How to create a String

There are two ways to creating a string, first is the direct way which is;
String x = "This is the newly Created string";
System.out.print(x);
The "" in the declaration combines all the characters and saves them into the string literal x. Then the other way around which is from converting a character array into a string.
Here's how;

char[] Hello = {'H','e','l','l','o'}; //This creates a character array called Hello
String x = new String(Hello); // This creates a new string from the array of characters above
System.out.print(x); //Outputs the string x

This is basically how to create a string, now to the methods in the string class which we'll be studying.

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value. Source

There are a few methods in the Java String Class and we will be visiting four of them in this tutorial. They Include

  • codePointAt();
  • codePointBefore();
  • codePointCount();
  • startsWith();

To begin with we'll be understanding what each method does before using them.

  • codePointAt(index);: This method is used to know the Unicode point value of a character in the string specified by an index value which is entered in the () parenthesis.
    For example the Unicode point value of the Character 'A' is 65 and that of 'H' is 104 so if the character at the selected index is any of these, it will return their value as stated. Here's how to use the method;
public class Stringclass {
 public static void main(String [] args){
String x = "This is the new String"; // This creates the string
System.out.println(x.codePointAt(1));  // This prints out the Unicode code value of the character at the selected index 1.
 }       
}

The code above will output the value 104 as seen in the screenshot below
image.png

  • codePointBefore(index);: This method returns the Character Unicode Code point before the chosen index, for example if the index selected is 3 then the Character Unicode code point will be taken from index 2.
String x = "This is the new String"; //This creates a new String
System.out.println(x.codePointBefore(1)); //This prints the Character Unicode code point at index 0 as output.

If we run the code above, we get the value 84 as output, just as in the screenshot below.
image.png

  • codePointCount(index 1,index 2);: This method returns the number of Unicode Code Points between the selected indices. In using this method, you have to select two indexes that can be subtracted from each other and still have a number higher than 0, else the result is 0 also. If two index points are not input in the code, it will not run. Here's what the code is meant to look like;
    System.out.println(x.codePointCount(1,5)); if you have written the code correctly your output will read 4, just as seen in the screenshot below.
    image.png
  • startsWith(string or char): This method is used to test if the created string starts with a character or another string prefix. It returns a Boolean value, which is trueor false;
    the syntax for this method looks like this
    Boolean startsWith(String prefix); an example can be seen in the screenshot below where the string is tested to start with the prefix "Th" and it returned the value true;
    image.png
    This same method is then tested with the wrong prefix and it returns the value false;
    image.png

In conclusion. there are few more methods in the java string class which we will be visiting in coming tutorials, but for now I'll advice that these four are studied as they can be applied at any point in programing.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  
  • the better usage of the function "startsWith" could be found elsewhere
  • the better usage of the function "codePointAt" could be found elsewhere
  • the better usage of the function "codePointBefore" could be found elsewhere
  • the better usage of the function "codePointBefore" could be found elsewhere

Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.

[utopian-moderator]