Home » Posts tagged 'string'

Tag Archives: string

String Library Function

Now list some string function mostly used in PHP program to handle string.
sterlen() Length of String

sterlen() function is used to get the entire length of the string. If there is space in between two words then it will count that space as well.



str_word_count() -Word Count
str_word_count() function will give you the exact number of words that a string have.



strrev() -String Reverse
strrev() function is used to reverse the entire string.


trim() -String Remove
Remove whitespace (or other characters) from the beginning and end of a string

Another example

ltrim() => Remove whitespace (or other characters) from the beginning of a string
rtrim() => Remove whitespace (or other characters) from the end of a string

ucfirst()- Convert first letter into uppercase
convert first letter into uppercase of any string

strtoupper() -to upper case
This function converts the string in upper case

strtolower() -Converts string to lowercase:
convert whole string into lowercase

chr() -Return character from ASCII code
returns any character or letter and also special character from their equivalent ASCII Code

implode() -array to string
Implode function combine all array element with a string

explode() -string to an array
Explode function split a string and return all value into an array.

strpos() -find the text position in string

strpos() function is used to find the particular text in a string. If while searching in the string it founds the text, then it will give the position of that word in first search match or if the particular text is not found then it will return FALSE value. Also first position of any character in string is 0 not 1.


str_replace() -Replace string

str_replace() function is used to replace some characters that are there in the strings.



PHP String

String means set of characters or letters or combination in single or double quotes. Like ‘PHP Language’. If you placed the number in the single quote or double quotes it’s also a string. Like ‘12345’. Similarly, special characters symbol whatever if you write it in the single or double quote it’s become a string.
In PHP a single letter or character also a string. Like ‘A’

Creating and accessing String

In PHP language whatever you write in a code with a single or double quotation, it creates the string.
To declare a string always use the quotation, if you forget to mention quotes give you an error.

Syntax of String

Note: gettype() is a predefine PHP function which used to know the datatype of any variable. In this above example gettype() function return string.



PHP is a very good language and easy to learn And it supports many other languages like HTML, CSS, JAVASCRIPT and so on.

You can write a string with single quotes or double quotes both are equal. In above example $var and $var2 both hold string value.

Another Syntax of String:


In the first example, we want to print I’d buy myself, in this case, we never declare this string with the single quote because in the original value there has a single quote already. Means in PHP language you never used the same type of quotes so start with the double quote.
In the second example, it returns Sum of 2 & 4 is $sum because we declare the string with single quote and variable also treated as a string in the single quote so it’s can’t return the value of $sum. So never used the variable in a single quote
But in the third example, we receive the value of $sum because double quote treats a variable like a variable so we can receive the value of any variable, not the variable name.
And in the fourth example, we used two type of quotation to declare a string. Because above example, we need to use nested quotes under the main quotes. And in PHP language does not support the same type of quotes as a nested quote.
So in this type of case start with the single quote and used the double quote as a nested quote or start with double quotes and used the single quote as a nested quote.
If you don’t maintain these things PHP gives you an error.

Avoiding the single quote double quote complexity you also used escape sequences (\). PHP used \ symbols as an escape sequence.
Using this escape sequence you also rewrite those strings like this,

Syntax of String with \:

Another way to Create String (HereDoc):
In complicated cases, you need to create the large string. And there you have to follow all rules of quotation or escape sequence to avoid error.
In PHP here has a new way to write the complex string with the simple manner.
3.1 Syntax of HereDoc:


This is another way of showing String Delimiters The value is Hello World
This is another way of showing String Delimiters The value is Hello World

<<< is a heredoc symbol. After <<< symbol an identifier is provided, then a new line. Then you write your string and complete the string the same identifier again to close. The closing identifier must be same as beginning in the first line and the identifier must follow the same case. Identifier only starts with character or string or underscore (_) means it must contain only alphanumeric characters. In above example, EOF and tutorial are the identifiers.

Java String concat() method

String concat() is used to concatenate two strings. The parameter str string is appended to specified string and new string object is returned by this method.
“Hello ”.concat(“Friends”)
This will generate a new string object “Hello Friends”


Str – is string parameter used to concatenate with specified string


Here str is concatenated with ” ” then it is concatenated with str1 and result is assigned to str3 String object.

Java String substring() method

This method takes a int argument to generate sub string of string. It returns sub string starting from beginIndex value to last of string.
The second overloaded method allows to specify the ending index value also.
beginIndex parameters is inclusive and endIndex is exclusive.


public String substring(int beginIndex)
public String substring(int beginIndex,int endIndex)


beginIndex –generate substring from this index value(inclusive).
endIndex-generate substring up to endIndex-1 value(exclusive).


A substring


IndexOutOfBoundsException: If beginIndex is negative or endIndex is greater then length of string or if beginIndex is greater then endIndex.



Java String Method compareToIgnoreCase()

This method is used to compare two string with case ignorance and compare both string lexicographically. If both strings are same then it returns 0. If first string is greater then another then it returns positive integer, if second string is greater then first then it returns negative integer value.


public int compareToIgnoreCase(String str)


str – String value that will compared with string.


Integer value. if string is greater then passed str value then it returns positive integer if both strings are equal then it returns zero if passed str is greater then string then this method returns negative integer value.



In above program comparing same string is returning 0. In second println() statement A is lexicographically smaller then b so it is returning -1. In third println() statement C is lexicographically greater then b so it is returning 1 and so on.



Java String Method charAt()

This method takes one integer argument and return a character specified at index position. For any string the index value starts from 0 to string length -1 position. If passed index value not in valid range then it throws IndexOutOfBoundsException.

For example string Hello Friends contains 13 characters starting from 0 to 12 H is first position with index value 0, F is in seventh position with index value 6 and s is in 13 position with index value 12.
Using numbre other then 0 to 12( 0 to length()-1) will result in IndexOutOfBountException


public char charAt(int index)


index -Integer index value to get character at specified index position.


Character at passed index value.

Exception Throws:

IndexOutOfBoundsException If index value in not between 0 to String length()-1



Now Print all characters with its index value



Managed by MochaHost - Fast & Easy WordPress Hosting