Inbuilt Functions
Inbuilt Functions
Objectives
• Inbuilt Functions
• String Functions (The Mid, Right, Left & Trim methods)
• String Manipulation Using + and & signs.
• Mathematical Functions (Abs, Exp, Fix functions)
• Formatting Functions (ConvertToString, ConvertToInt16… Methods; Date and
Time Formatting functions)
• User-Defined Functions
• Passing parameters by value & by reference
Inbuilt Functions
• In C# programming language, inbuilt functions are predefined functions
that are provided by the .NET framework or other libraries.
• These functions are ready to use and can be called directly in your code
without the need to define them yourself.
• Inbuilt functions are designed to perform common tasks or operations,
such as mathematical calculations, string manipulation, file handling, and
more.
• Some examples of inbuilt methods in C# include:
• `Console.WriteLine()` for outputting text to the console,
• `Math.Max()` for finding the maximum of two numbers, and
• `String.ToUpper()` for converting a string to uppercase.
String Functions
• Here are some common methods of the `String` class in C#:
1. `Mid / Substring(int startIndex)`: Returns a substring starting from the specified index.
2. `Trim()`: Removes leading and trailing white spaces from the string.
3. `Right`: Get specific number of characters (count) from right side
4. `Left`: Get specific number of characters (count) from left part
5. `Length`: Returns the length of the string.
6. `ToUpper()`: Converts the string to uppercase.
7. `ToLower()`: Converts the string to lowercase.
8. `Contains(string value)`: Checks if the string contains a specified value.
9. `IndexOf(char value)`: Returns the index of the first occurrence of the specified character.
Left, right, and mid strings
• Unlike other programming languages C# doesn’t have special
methods for the left, right or mid part of a string but we can still
implement that behavior.
• We do so using the C#’s substring method
Left part of string
string.Substring(0, count)
• 0 arg: specifies we are starting from the left side.
• Count arg: specifies the number of characters from the string’s left
side
string example = "Hello, World!";
// Get the 5 characters from the left of the string
string exampleLeft = example.Substring(0, 5);
// Output
H E L L O J O S E
• String.Substring(string.Length - 4, 4);
string example = "HELLO JOSE";
// Get 4 characters from the right of the string
string exampleRight = example.Substring(example.Length - 4, 4);
// Output Console.WriteLine($"Original string: {example}");
Console.WriteLine($"Right string: {exampleRight}");
• First Version
string example = "Hello, World!";
// Get all characters from the 4th character onward
string exampleMid = example.Substring(4); \
// Output
Console.WriteLine($"Original string: {example}");
Console.WriteLine($"Mid string: {exampleMid}");
• Output
Original string: Hello, World!
Mid string: o, World!
Mid Part of String
• Second Version:
string example = "Hello, World!";
// Get 6 characters from the string, starting at character 4
string exampleMid = example.Substring(4, 6);
// Output
Console.WriteLine($"Original string: {example}");
Console.WriteLine($"Partial mid string: {exampleMid}");
• Output:
Original string: Hello, World!
Partial mid string: o, Wor
Trim()
• Illustrating the trim method:
string name = “ joseph“;
string name1.Trim();
String Concatenation using + operator
string str1 = "Hello";
string str2 = "World";
//The following line Concatenates two strings with a space in between
string result = str1 + " " + str2;
Console.WriteLine(result);
• // Output:
Hello World
String Manipulation Using + operator
string str = "Hello";
str += " World"; // Appends " World" to the existing string
Console.WriteLine(str);
// Output:
Hello World
String Manipulation using the & operator
• string str = "Hello";
• str &= " World"; // Concatenates " World" to the existing string
• Console.WriteLine(str);
• // Output:
Hello World
Mathematical Functions (Abs, Exp, Fix functions)
// Output: 10
Mathematical Functions (Abs, Exp, Fix functions)
• ConvertToString method:
• The Convert.ToString method is used to convert a value to its string
representation.
1. int num = 10;
2. string str = Convert.ToString(num);
3. Console.WriteLine(str);
4. // Output: "10"
Formatting Functions (ConvertToString,
ConvertToInt16… Methods; Date and Time
Formatting functions)
• Date and Time Formatting functions:
• C# provides various methods for formatting date and time values using the
DateTime structure and ToString method
1. DateTime thisDay= DateTime.Now;
2. string formattedDate = thisDay.ToString("yyyy-MM-dd HH:mm:ss");
3. Console.WriteLine(formattedDate);
// Output: "2023-03-08 07:30:45"
• ConvertToInt16 method:
• The Convert.ToInt16 method is used to convert a value to a 16-bit signed integer.
1. string str = "123";
2. short num = Convert.ToInt16(str);
3. Console.WriteLine(num);
// Output: 123
User-Defined Functions
Passing parameters by value & by reference
• In C#, you can define user-defined functions to encapsulate a block of code that performs a
specific task.
• Parameters can be passed either by value or by reference
• NOTE:
• When using ref, the parameter must be initialized before passing it to
the function, and the function can read and modify the parameter.
• When using out, the parameter does not need to be initialized before
passing it to the function, but the function must assign a value to the
parameter before it returns.