Course Content
State Management
0/1
Regular Expressions?
0/1
About Lesson

What are string operations :

String operations in programming involve various manipulations and transformations performed on strings, which are sequences of characters. Below are brief explanations of common string operations:

1. Concatenation:

Concatenation involves joining two or more strings together to create a single string. In PHP, concatenation is performed using the . (dot) operator.

Example:

PHP
<?php

$str1 = "Hello";

$str2 = " world!";

$result = $str1 . $str2;

?>

// Output: Hello world!

2. Substring:

A substring is a portion of a string. Substring operations involve extracting a portion of a string based on specified start and end positions or lengths.

Example:

PHP
<?php

$str = "Hello world!";

$substring = substr($str, 6, 5);

?>

// Output: world

3. Length:

Length operations involve determining the number of characters in a string. In PHP, the strlen() function is used to get the length of a string.

Example:

PHP
<?php

$str = "Hello world!";

$length = strlen($str);

?>

// Output: 12

4. Searching:

Searching operations involve finding specific substrings or characters within a string. In PHP, functions like strpos() or strstr() are used to search for substrings.

Example:

PHP
<?php

$str = "Hello world!";

$pos = strpos($str, "world");

?>

// Output: 6

5. Replacement:

Replacement operations involve replacing occurrences of a substring within a string with another substring. In PHP, functions like str_replace() or preg_replace() are used for string replacement.

Example:

PHP
<?php

$str = "Hello world!";

$newStr = str_replace("world", "PHP", $str);

?>

// Output: Hello PHP!

6. Case Conversion:

Case conversion involves changing the case of characters within a string. In PHP, functions like strtolower() or strtoupper() are used to convert characters to lowercase or uppercase, respectively.

Example:

PHP
<?php

$str = "Hello World!";

$lowercase = strtolower($str);

?>

// Output: hello world!

These are some of the fundamental string operations used in programming languages like PHP. Understanding and utilizing these operations effectively allows developers to manipulate and process strings according to their application requirements.