String interpolation is the process of substituting or replacing the variables in the placeholders that are present inside the string. It is a process of injecting the values into the placeholder; a placeholder is just a variable in which we can assign values later in the string at execution. But the existence of the placeholder variable is nothing until any value is assigned to it. This tutorial guide will explain the working of string interpolation in C sharp.
Implementation of String Interpolation
Example 1
The first example of string interpolation is to find the area of two integer type values. These values are replaced by the placeholder we use in the formula. There are always some methods of using the placeholders along with the symbols that incorporate the variables and the placeholders. For instance, we have used the ‘@’ and a Dollar ‘$’ sign here. These signs assist the usage of string interpolation.
So while implementation, now in the source code, use the system library first to use classes and other containers. Three integer data type variables are declared. Two of them are declared with values, whereas the third one stores the resultant value. The base and height are declared, whereas the area will be calculated by providing the formula. By symbol interpolation, we have used two symbols by having an order of @ appear before $.
# Console.Writeline(@ $ "Area = " " {area} " " ");
After writing the source code in the file, we will compile it in the terminal. There is always a compiler and an executor that compiles and executes the code. So we use MCS to compile the code of C sharp in the Linux operating system.
An error exists while compiling the code, and the compilation process is failed with 2 errors. These errors show that the order of keywords and the symbols for string interpolation is inaccurate.
We must follow the correct order while using two symbols in the string interpolation process. Otherwise, an error will occur, as shown above. These errors can be rectified by changing the order; we have used the ‘@’ sign before the dollar symbol. So now we will use the ‘$’ sign before the ‘@’.
So using the same example, we will come up with a different approach to the order of the symbols. All the values and formula variables will be the same. This time we have written ‘$’ before ‘@’, this has an impact on the replacement of the placeholder with the value of a variable.
# Console.Writeline($ @ "Area = " " {area} " " ");
Now save the code, and we will compile it with MCs, whereas Mono is used to execute the code.
$ mono file.exe
Now while execution, you have seen that the compilation was smooth and both the errors have been rectified, both values are displaced as they have replaced the placeholders, and the area is also calculated through the formula.
Example 2
This example deals with interpolation searching. First, we will use the algorithm to explain the working of this process of searching.
Algorithm
- There exists a formula to calculate the position of the item.
- If we find the match through the formula, we will return the item’s index and the value.
- If the item to be found is less than the position of the array index right now, arr[pos], then calculate the probe position again for the left side sub-array. On the other hand, calculate the position for the right side sub-array if the item is greater.
- We will repeat the process until a match is found or the sub-arrays reduce to zero.
- The prerequisite for this searching process is that the array must be sorted.
Now we will implement this algorithm. A function is created for the searching process; this will take the string array, the element we want to search, and the lower and the higher position number. An integer data type “pos” variable is declared to find the position.
If the value is present inside the array, it returns the index and the number to show the index number at which the value is present; on the other hand, it returns -1.
Using an if-statement will check whether the element is present or not, as the array is sorted, so it must lie in the specified range as defined by the corner. Inside the if-statement, the position is calculated by dividing the array into two sub-arrays while keeping the uniform distribution.
This formula indicates that, by subtracting the lower point from the highest index and then adding the lowest number, the result is then divisible by the value at the lowest index and subtracted by the highest one; after obtaining the value by doing so, this is then multiplied by the number obtained by subtracting the number at lowest index from the number to be searched.
Then again, if the statement will check if the number is found by comparing it with the resultant of the value obtained, return the number. And if the number to be found is greater than the value obtained, it means the number is in the right sub-array. Again, this will lead to the function call by having the number to be searched, only the highest position, and the mid/position plus 1, from where the right portion is started.
# Return interpolationsearch (arr, pos + 1, hi, x)
And if the number is smaller than the mid-point, the left array is considered.
Otherwise, return the -1 value. In the main program, a string array is declared with 6 values. These values are in ascending order, as we need a sorted array as input. Again, we will declare a variable containing the value we want to search. The length of the array will be obtained from the built-in function. We will call a function and a variable to store the value returned from the function.
Save the code and then execute it; you will see that the number 13 searched is present on the second index in the array. The index number is displayed by replacing the index variable at the display time.
Conclusion
String interpolation is the mechanism of incorporating the values in the variable in the location of the placeholders present inside the string. We have represented the string interpolation in the Linux system using two symbols concerned with the order of representation. If the order of any one of the statements is changed, the error will occur. Moreover, we have also done interpolation searching in the string array. In this way, we can use a string interpolation concept in many ways.