Friday, February 26, 2016

System.out.print("Strings");

Strings are identifiers that are composed of one or many char identifiers, they can hold a big amount of characters and can be modified within a program in an amount of ways. a usual string is created in this way: String str1 = "Hello World";   It is very important that String is capitalized, that it's variable is stated with a quotation mark for both starting and beginning the string and that it ends with a semicolon.
Print method.
The print method is a method from the java.util.Object class in java that is the main class from which you import statements and methods from. It allows you to use String, String manipulation methods, the Print methods, and other equally important methods.
The print method is stated like this:
System.out.print("This is a print");
This will print the words "This is a print" to console. This is often used outside of the Graphic interfaces of programs for debugging purposes.
There are different methods that are used to manipulate String objects: charAt(), substring(), contains(), indexOf(), concat(), equals(), length(), trim(), etc.
These are used for different purposes, charAt(int) will find a char type data at a certain index from the string on which is used, it has parameters of int which means that you need to introduce an int and it will find the character from that position.
The method substring(int, int) requires two integers one for the beginning of the sub string and one for the end, it will take a chunk from between the two given indexes.
The method contains(String) has a String parameter and will find if the string it's used on has a sub string with the exact composition of the parameter string and will give it back as a boolean.
To find an index of a sub-string or a char then you use the indexOf(String) method which will return an int.
The method concat(String) allows you to concatenate two strings together, it will add the parameter string into the string to which the method is used.
equals(String) is the most secure way to see if two strings are equal to each other instead of using the == identifier.
length() will return an int with the number of characters that are in a single string variable.
trim() will erase from a string any excess white space from before or after a string.
REMEMBER: every time you modify a String object the String class does not dump the previous object instead it creates a new one and saves the other one.

String a = "\'a small fox sleeps\'"; if(a.contains("fox")){System.out.print(a);}
This will print: 'a small fox sleeps' to the console.

Apart from manipulating Strings with those three methods it is also possible to turn other primitive data to Strings. The method toString() from the class of any primitive data will turn it to Strings:
Integer.toString(), Double.toString(), Boolean.toString(), etc.

1 comment:

  1. Another informative blog from you Cesar! Thanks for that!

    ReplyDelete