String methods

string-methods

Substring – returns substring into substring.

Example

Code

var longString = "This is very long string";
Console.WriteLine(longString.Substring(5));

Result

is very long string

Replace() – replace the specified old character with the specified new character.

Example

Code

var longString = "This is very stringman";
Console.WriteLine(longString.Replace("is", "IS"));
Console.WriteLine(longString.Replace("very", "VERY"));
Console.WriteLine(longString.Replace("iS", "IS", StringComparison.CurrentCultureIgnoreCase));;

Results

ThIS IS very stringman
This is VERY stringman
ThIS IS very stringman

Join() – joins the given string using the specified separator.

Example

Code

string[] words = {"apple", "banana", "cherry"};
string result = string.Join("-", words);
Console.WriteLine(result);

string[] words2 = {"ananas", "ball"};
string result2 = string.Join("*", words2);
Console.WriteLine(result2);

Results

apple-banana-cherry
ananas*ball

Remove() – returns character from a string.

Example

Code

string tekst = "Hello world";
string tekst1 = tekst.Remove(2, 7);
Console.WriteLine(tekst1);

Results

Held

PadLeft – returns string padded with space or with a specified Unicode character.

Example

Code

string tekst = "Hello";
string tekst1 = tekst.PadLeft(10, '0');
Console.WriteLine(tekst1);

Results

00000Hello

PadRight – returns string padded with space or with a specified Unicode character.

Example

Code

string tekst = "Hello";
string tekst1 = tekst.PadLeft(10, '0');
Console.WriteLine(tekst1);

Results

Hello00000

ToCharArray = converts the string to a char array.

Example

Code

string tekst = "Hello";
char[] charArray = tekst.ToCharArray();

foreach(char c in charArray)
{
    Console.WriteLine(c);
}

Results

H
e
l
l
o

LastIndexOf() – returns index the last occurance of specified string.

Example

Code

string tekst2 = "The quick brown fox jumps over the lazy dog!";
int index = tekst2.LastIndexOf("o");
Console.WriteLine(index);

Results

41
Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
6-underrated-keyword-research-methods-—-whiteboard-friday

6 Underrated Keyword Research Methods — Whiteboard Friday

Next Post
ascii-art-code-in-rust

ASCII Art Code in Rust

Related Posts