|
| 1 | +--- |
| 2 | +Title: '.Substring()' |
| 3 | +Description: 'Returns the substring of a string instance starting at a given index.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Methods' |
| 9 | + - 'Strings' |
| 10 | +CatalogContent: |
| 11 | + - 'learn-c-sharp' |
| 12 | + - 'paths/computer-science' |
| 13 | +--- |
| 14 | + |
| 15 | +The **`.Substring()`** method is a string method that returns a substring of a string starting at the specified index. It will return all characters from that index to the end unless a maximum length is specified. If the starting index equals the string length, it returns an empty string (`""`). If the index is greater than the string length, it throws an `ArgumentOutOfRangeException`. |
| 16 | + |
| 17 | +## Syntax |
| 18 | + |
| 19 | +```pseudo |
| 20 | +.Substring(int startIndex) |
| 21 | +``` |
| 22 | + |
| 23 | +Or, alternatively: |
| 24 | + |
| 25 | +```pseudo |
| 26 | +.Substring(int startIndex, int length) |
| 27 | +``` |
| 28 | + |
| 29 | +- `startIndex`: The index from where the substring starts. |
| 30 | +- `Length` (Optional): The number of characters to include in the substring.. |
| 31 | + |
| 32 | +## Example |
| 33 | + |
| 34 | +In this example, `.Substring()` is used to return the substring of "Codecademy" starting at index `4` and includes all characters from that position to the end of the string: |
| 35 | + |
| 36 | +```cs |
| 37 | +using System; |
| 38 | + |
| 39 | +public class Program |
| 40 | +{ |
| 41 | + public static void Main() |
| 42 | + { |
| 43 | + string str = "Codecademy"; |
| 44 | + Console.WriteLine(str.Substring(4)); |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +The above example results in the following output: |
| 50 | + |
| 51 | +```shell |
| 52 | +cademy |
| 53 | +``` |
| 54 | + |
| 55 | +## Example 2 |
| 56 | + |
| 57 | +In this example, `.Substring()` is used with the optional `length` parameter to return a substring of 6 characters starting from index `2` of the string `"Codecademy"`. |
| 58 | + |
| 59 | +```cs |
| 60 | +using System; |
| 61 | + |
| 62 | +public class Program |
| 63 | +{ |
| 64 | + public static void Main() |
| 65 | + { |
| 66 | + string str = "Codecademy"; |
| 67 | + Console.WriteLine(str.Substring(2, 6)); |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +The above code generates the following output: |
| 73 | + |
| 74 | +````shell |
| 75 | +decade |
| 76 | + |
| 77 | +## Codebyte Example |
| 78 | + |
| 79 | +The below code demonstrates how to use the Substring method: |
| 80 | + |
| 81 | +```codebyte/csharp |
| 82 | +using System; |
| 83 | +
|
| 84 | +public class Example |
| 85 | +{ |
| 86 | + public static void Main(string[] args) |
| 87 | + { |
| 88 | + string Name1 = "Brad"; |
| 89 | + string Name2 = "Angelina"; |
| 90 | +
|
| 91 | + Console.WriteLine(Name1.Substring(1)); |
| 92 | + Console.WriteLine(Name2.Substring(1)); |
| 93 | + } |
| 94 | +} |
| 95 | +```` |
0 commit comments