-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
96 changed files
with
5,579 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
content/ruby/concepts/variables/variables.md | ||
content/ruby/concepts/gems/gems.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
Title: '.Join()' | ||
Description: 'Joins elements of a collection into a single string with a specified separator.' | ||
Subjects: | ||
- 'Code Foundations' | ||
- 'Computer Science' | ||
Tags: | ||
- 'Strings' | ||
- 'Methods' | ||
- 'Characters' | ||
CatalogContent: | ||
- 'learn-c-sharp' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
The **`.Join()`** method concatenates an array or a collection of strings into a single string, with a specified separator inserted between each element. | ||
|
||
This method is particularly useful for formatting lists, such as comma-separated values or readable sentences. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
string.Join(string separator, IEnumerable<string> values) | ||
``` | ||
|
||
- `separator`: A string to insert between each element. If empty, elements are joined with no separator. | ||
- `values`: An array or collection of strings to concatenate. `null` values in the collection are replaced with empty strings. | ||
|
||
## Example | ||
|
||
This example demonstrates how `.Join()` uses a comma and space (`,`) as the separator to create a formatted to-do list. | ||
|
||
```cs | ||
using System; | ||
|
||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string[] studyTasks = { "Complete C# lesson", "Review notes", "Practice coding exercises", "Read documentation" }; | ||
|
||
// Using .Join() to create a sequential to-do list with ", " as the separator | ||
string toDoList = string.Join(", ", studyTasks); | ||
|
||
Console.WriteLine("Today's Codecademy study plan: " + toDoList); | ||
} | ||
} | ||
``` | ||
|
||
This example results in the following output: | ||
|
||
```shell | ||
Today's Codecademy study plan: Complete C# lesson, Review notes, Practice coding exercises, Read documentation | ||
``` | ||
## Codebyte Example | ||
The following example demonstrates how `.Join()` combines a collection of course names using an ampersand (`&`) to produce a readable list. | ||
```codebyte/csharp | ||
using System; | ||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
string[] languages = { "C#", "Python", "Java" }; | ||
string codecademyCourses = string.Join(" & ", languages); | ||
Console.WriteLine("Popular Codecademy courses: " + codecademyCourses); | ||
} | ||
} | ||
``` |
118 changes: 118 additions & 0 deletions
118
content/c-sharp/concepts/strings/terms/padright/padright.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
--- | ||
Title: '.PadRight()' | ||
Description: 'Adds padding to the right side of a string to ensure it reaches a specified total length.' | ||
Subjects: | ||
- 'Code Foundations' | ||
- 'Computer Science' | ||
Tags: | ||
- 'Strings' | ||
- 'Characters' | ||
- 'Methods' | ||
- 'Functions' | ||
CatalogContent: | ||
- 'learn-c-sharp' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
The **`.PadRight()`** method in C# is used to add padding characters to the right of a string to achieve a specified total length. By default, it uses spaces as the padding character, but a custom character can also be specified. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
string.PadRight(int totalWidth, char paddingChar) | ||
``` | ||
|
||
- `totalWidth`: The desired total length of the string, including padding. If the specified width is less than the string's length, no padding is added. | ||
- `paddingChar`(Optional): The character to use for padding. Defaults to a space character (`' '`). | ||
|
||
> **Note**: The `.PadRight()` method does not modify the original string. It generates and returns a new string with padding applied to achieve the specified total width. | ||
## Example | ||
|
||
### Default Padding with Spaces | ||
|
||
```cs | ||
using System; | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string name = "John"; | ||
string paddedName = name.PadRight(10); | ||
Console.WriteLine($"'{paddedName}'"); | ||
} | ||
} | ||
``` | ||
|
||
The above code generates the output as follows: | ||
|
||
```shell | ||
'John ' | ||
``` | ||
|
||
### Custom Padding Character | ||
|
||
```cs | ||
using System; | ||
|
||
class NameFormatter | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
string name = "John"; | ||
string paddedName = name.PadRight(10, '-'); | ||
Console.WriteLine($"'{paddedName}'"); | ||
} | ||
} | ||
``` | ||
|
||
The output of the above code will be: | ||
|
||
```shell | ||
'John------' | ||
``` | ||
|
||
### Handling Shorter Total Width | ||
|
||
If the specified `totalWidth` is less than the length of the string, the original string is returned: | ||
|
||
```cs | ||
using System; | ||
|
||
class NamePaddingExample | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
string name = "John"; | ||
string result = name.PadRight(3); | ||
Console.WriteLine($"'{result}'"); | ||
} | ||
} | ||
``` | ||
|
||
Here's what the output of this code will be: | ||
|
||
```shell | ||
'John' | ||
``` | ||
|
||
## Codebyte Example | ||
|
||
Run the following example to understand how the `.PadRight()` method works: | ||
|
||
```codebyte/csharp | ||
using System; | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string word = "Align"; | ||
// Pad with spaces to a total width of 15 | ||
Console.WriteLine("'" + word.PadRight(15) + "'"); | ||
// Pad with asterisks to a total width of 15 | ||
Console.WriteLine("'" + word.PadRight(15, '*') + "'"); | ||
} | ||
} | ||
``` |
95 changes: 95 additions & 0 deletions
95
content/c-sharp/concepts/strings/terms/substring/substring.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
--- | ||
Title: '.Substring()' | ||
Description: 'Returns the substring of a string instance starting at a given index.' | ||
Subjects: | ||
- 'Code Foundations' | ||
- 'Computer Science' | ||
Tags: | ||
- 'Methods' | ||
- 'Strings' | ||
CatalogContent: | ||
- 'learn-c-sharp' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
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`. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
.Substring(int startIndex) | ||
``` | ||
|
||
Or, alternatively: | ||
|
||
```pseudo | ||
.Substring(int startIndex, int length) | ||
``` | ||
|
||
- `startIndex`: The index from where the substring starts. | ||
- `Length` (Optional): The number of characters to include in the substring.. | ||
|
||
## Example | ||
|
||
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: | ||
|
||
```cs | ||
using System; | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
string str = "Codecademy"; | ||
Console.WriteLine(str.Substring(4)); | ||
} | ||
} | ||
``` | ||
|
||
The above example results in the following output: | ||
|
||
```shell | ||
cademy | ||
``` | ||
|
||
## Example 2 | ||
|
||
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"`. | ||
|
||
```cs | ||
using System; | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
string str = "Codecademy"; | ||
Console.WriteLine(str.Substring(2, 6)); | ||
} | ||
} | ||
``` | ||
|
||
The above code generates the following output: | ||
|
||
````shell | ||
decade | ||
|
||
## Codebyte Example | ||
|
||
The below code demonstrates how to use the Substring method: | ||
|
||
```codebyte/csharp | ||
using System; | ||
public class Example | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
string Name1 = "Brad"; | ||
string Name2 = "Angelina"; | ||
Console.WriteLine(Name1.Substring(1)); | ||
Console.WriteLine(Name2.Substring(1)); | ||
} | ||
} | ||
```` |
Oops, something went wrong.