Strings of Java and PHP
This is the fourth part of the Introduction to Java and PHP series. String objects of Java and PHP will be discussed. Whenever possible, code examples will be made in a similar manner for both Java and PHP.
Strings in Java and PHP are very similar. Variables in Java and PHP can declared and assigned values at the same time.
In this tutorial, Java and PHP will be used to create a program consisting of string expressions. The program will be compiled and executed. In order to make the work-flow faster, please use a programming editor.
- Tools are required:
- Text editor for creating and modifying the Java and PHP files.
- Java.
- PHP.
Optional Download and install suitable text editor
Any text editor will work. A bare minimum should be syntax-highlighting for Java and PHP. To select a reviewed lightweight programming editor read the Ojambo.com Lightweight Programming Editors.
Lesson 15: Declare String Variables
Java | PHP |
---|---|
String string1 = new String(); // String String string1 = “abc”; // String String string2 = “def”; // String String string3 = ” leading and trailing whitespace “; // String |
$string1; // String $string1 = “abc”; // String $string2 = “def”; // String $string3 = ” leading and trailing whitespace “; // String |
In Java, strings are constant and represents by the class “String”. Java also requires the variable’s first letter to be lowercase.
PHP only requires the variable to have a dollar sign at the beginning.
Lesson 16: String Copy
Java | PHP |
---|---|
String stringCopy = new String(string1); // String as new variable | $stringCopy = $string1; // String as new variable |
In Java, the new string variable is created as a new object. In PHP, a new string variable only requires the dollar sign at the beginning.
Lesson 17: String Concatenation
Java | PHP |
---|---|
String stringCat = string1 + string2; // String concatenation | $stringCat = $string1 . $string2; // String concatenation |
For Java, the string concatenation operator is the plus sign “+”. In PHP, the string concatenation operator is the period sign “.”.
Lesson 18: String Manipulation
Java | PHP |
---|---|
Substring (String Substitute) | |
String stringSub = string1.substring(2,3); // Substring(from, to) | $StringSub = substr($string1, 2,3); // Substr(string, start, length) |
Uppercase | |
String stringUpper = string1.toUpperCase(); // Uppercase | $stringUpper = strtoupper($string1); // Uppercase |
Trim (Remove Whitespace) | |
String stringTrim = string3.trim(); // Trim whitespace | $stringTrim = trim($string3); // Trim whitespace |
Java uses substring with a specified start index and an optional end index for string substitution. PHP uses substr with a specified string, start, and an optional length for string substitution. For string substitution, both Java and PHP use integers to specify the start and length.
Java uses toUpperCase to convert a string to uppercase. PHP uses strtoupper with a specified string in order to convert a string to uppercase.
Java uses trim to remove leading and trailing whitespace. PHP uses trim with a specified string in order to remove leading and trailing whitespace.
StringObjects.java file
/** * Ojambo.com String Objects Java Tutorial * StringObjects.java Copyright 2011 Edward * http://www.ojambo.com * */ // This class displays String Objects public class StringObjects { //This class is called StringObjects public static void main(String[] args) { // Main Method // Declare variables and assign values immediately String string1 = "abc"; // String String string2 = "def"; // String String string3 = " leading and trailing whitespace "; // String // Concatenation String stringCat = string1 + string2; // Concatenate string1 plus string2 // String Substitution String stringSub = string1.substring(2,3); // Substring(from, to) // String Uppercase String stringUpper = string1.toUpperCase(); // Uppercase convert // Remove Whitespace String stringTrim = string3.trim(); // Trim whitespace // Print out the assigned variables; System.out.println("string1 is " + string1); // Print the variable string1 System.out.println("string2 is " + string2); // Print the variable string2 System.out.println("string3 is " + string3); // Print the variable string3 System.out.println("stringCat is " + stringCat); // Print the variable stringCat System.out.println("stringSub is " + stringSub); // Print the variable stringSub System.out.println("stringUpper is " + stringUpper); // Print the variable stringUpper System.out.println("stringTrim is " + stringTrim); // Print the variable stringTrim } }
The StringObjects.java file can be compiled as javac StringObjects.java and executed with java StringObjects.
StringObjects.php file
<?php /* * Ojambo.com String Objects PHP Tutorial * StringObjects.php Copyright 2011 Edward * http://www.ojambo.com * */ // Declare variables and assign values immediately $string1 = "abc"; // String $string2 = "def"; // String $string3 = " leading and trailing whitespace "; // String // Concatenation $stringCat = $string1 + $string2; // Concatenate string1 plus string2 // $substitution $stringSub = substr($string1,2,3); // Substr(string, start, length) // String Uppercase $stringUpper = strtoupper($string1); // Uppercase convert // Remove Whitespace $stringTrim = trim($string3); // Trim whitespace // These statements print the assigned variables echo "string1 is".$string1; // Print out the $string1 variable echo "string2 is".$string2; // Print out the $string2 variable echo "string3 is".$string3; // Print out the $string3 variable echo "stringCat is".$stringCat; // Print out the $stringCat variable echo "stringSub is".$stringSub; // Print out the $stringSub variable echo "stringUpper is".$stringUpper; // Print out the $stringUpper variable echo "stringTrim is".$stringTrim; // Print out the $stringTrim variable ?>
The StringObjects.php can be compiled as php -l StringObjects.php and compiled with php StringObjects.php
Conclusion:
Java variables must begin with a lowercase letter. In Java, every variable must be specifically declared while PHP only requires a dollar sign. Sometimes Java and PHP share the same String Objects such as the trim function.
Java and PHP have the same string functions with different names. For example, concatenation is the plus sign in Java, but a period sign in PHP. String substitution is handled by the substring object in Java and substr fucntion is PHP. In order to convert strings into uppercase, Java uses the toUpperCase object and PHP uses strtoupper function.
- Recommendations:
- This tutorial uses an optional lightweight programming editor with syntax-highlighting features.
- The concepts taught in the first eighteen lessons can be applied to other programming languages.
- When selecting a programming language to master, pay close attention to license restrictions.
- Java and PHP Series:
- Ojambo.com Lightweight Programming Editors.
- Introduction to Java and PHP Part 1.
- Primitive data types for Java and PHP Part 2.
- Arithmetic Operators for Java and PHP Part 3.
- String Objects for Java and PHP Part 4.
- Control Structures for Java and PHP Part 5.
- Iteration Loops for Java and PHP Part 6.
- Array Types for Java and PHP Part 7.