C# 02 — String methods, For loop, Array, Foreach loop

Yen
2 min readJul 11, 2023

--

This is the note taken while I was learning C# from Bro Code’s Youtube Tutorials.

💧What we learn

  • string methods
  • for loop
  • array
  • foreach loop

🔥Good Resource

💠Learncs.org

💠Bro Code

💠GeeksforGeeks

⚡String methods

🔸ToUpper() & ToLower()

using System;

class Program
{
public static void Main(string[] args)
{

String name = "Alice Jin";
String upName = name.ToUpper();
String loName = name.ToLower();

Console.WriteLine(name);
Console.WriteLine(upName);
Console.WriteLine(loName);

Console.ReadKey();
}
}
/*
Alice Jin
ALICE JIN
alice jin
*/

🔸Replace

using System;

class Program
{
public static void Main(string[] args)
{

String call = "123-456-789";
Console.WriteLine(call);
call = call.Replace("-", "");
Console.WriteLine(call);

Console.ReadKey();
}
}
/*
123-456-789
123456789
*/

🔸Insert

using System;

class Program
{
public static void Main(string[] args)
{

String name = "Alice Jin";
name = name.Insert(0, "Miss ");
Console.WriteLine(name);

Console.ReadKey();
}
}
/*
Miss Alice Jin
*/

🔸Substring

using System;

class Program
{
public static void Main(string[] args)
{

String name = "Alice Jin";
name = name.Substing(0, 3);
Console.WriteLine(name);

Console.ReadKey();
}
}

⚡for loop

🔸Example #1

using System;

class Program
{
public static void Main(string[] args)
{

Console.WriteLine("Type rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Type columns: ");
int columns = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Type symbols: ");
String symbol= Console.ReadLine();

for (int i = 0; i < rows; i++){
for(int j =0; j< columns; j++){
Console.Write(symbol);
}
Console.WriteLine();
}

Console.ReadKey();
}
}
/*
Type rows:
2
Type columns:
5
Type symbols:
*
*****
*****
*/

⚡Array

🔸Example #1

using System;

class Program
{
public static void Main(string[] args)
{
/* Array Declaration #1 */
String[] fruits = { "Apple", "Avocado", "Lemon" };
/* Array Declaration #2 */
String[] cars = new String[3];
cars[0] = "Porsche";
cars[1] = "Tesla";
cars[2] = "Fox";
Console.WriteLine();


Console.ReadKey();
}
}

⚡foreach

🔸Example #1

This is for loop.

/* Array Declaration #1 */
String[] fruits = { "Apple", "Avocado", "Lemon" };
for (int i = 0; i < fruits.Length; i++)
{
Console.WriteLine(fruits[i]);
}

This is foreach loop.

String[] cars = new String[3];
cars[0] = "Porsche";
cars[1] = "Tesla";
cars[2] = "Fox";
foreach (String car in cars)
{
Console.WriteLine(car);
}

🟢Whole codes

using System;

class Program
{
public static void Main(string[] args)
{
/* Array Declaration #1 */
String[] fruits = { "Apple", "Avocado", "Lemon" };
for (int i = 0; i < fruits.Length; i++)
{
Console.WriteLine(fruits[i]);
}
/* Array Declaration #2 */
String[] cars = new String[3];
cars[0] = "Porsche";
cars[1] = "Tesla";
cars[2] = "Fox";
foreach (String car in cars)
{
Console.WriteLine(car);
}

Console.ReadKey();
}
}
/*
Apple
Avocado
Lemon
Porsche
Tesla
Fox
*/

--

--

Yen

Programming is a mindset. Cybersecurity is the process. Focus on Python & Rust currently. More about me | https://msha.ke/monles