0% found this document useful (0 votes)
37 views

OOP, C#

The document contains code examples demonstrating the use of various string methods in C# including String constructor, Length, indexing, ToUpper, ToLower, Substring, IndexOf, and LastIndexOf. The code shows how to initialize strings, get string lengths, access characters by index, convert case, extract substrings, find indexes of characters/substrings, and get the last index of a value in a string. The examples also contain explanations of the string methods in comments.

Uploaded by

16008119029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

OOP, C#

The document contains code examples demonstrating the use of various string methods in C# including String constructor, Length, indexing, ToUpper, ToLower, Substring, IndexOf, and LastIndexOf. The code shows how to initialize strings, get string lengths, access characters by index, convert case, extract substrings, find indexes of characters/substrings, and get the last index of a value in a string. The examples also contain explanations of the string methods in comments.

Uploaded by

16008119029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

// See https://aka.

ms/new-console-template for more information


using System.Net.Http.Headers;
/*
String Sınıfı
public String(char c, int count);
*/
namespace Nesne
{

class App
{
public static void Main()
{
String s = new String('a', 5);
Console.WriteLine(s); // aaaaa
}

}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public String(char c, int count);
*/
namespace Nesne
{

class App
{
public static void Main()
{
string s;
s = new string('a', 5);
Console.WriteLine(s); // aaaaa
}

}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public String(char c, int count);
*/
namespace Nesne
{

class App
{
public static void Main()
{
string s;
s = "Bursa";
Console.WriteLine(s); // Bursa
}

}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public String(char c, int count);
*/
namespace Nesne
{

class App
{
public static void Main()
{
string s;
s = "Bursa";
Console.WriteLine(s.Length); // 5
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public String(char c, int count);
*/
namespace Nesne
{

class App
{
public static void Main()
{
string s;
s = "Bursa";
Console.WriteLine(s[2]); // r
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public String(char c, int count);
*/
namespace Nesne
{
class App
{
public static void Main()
{
string s;
Console.Write("Bir yazı giriniz : ");
s = Console.ReadLine();
// Yazıyı tersten yazar
for(int i = s.Length - 1; i >= 0 ; i--)
Console.Write("{0}", s[i]);
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public String(char c, int count);
*/
namespace Nesne
{

class App
{
public static void Main()
{
string s;
s = "Ankara";
s[1] = 'k'; // Error
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
ToLower
ToUpper
*/
namespace Nesne
{

class App
{
public static void Main()
{
string s, k;
s = "Bursa";
k = s.ToUpper();
Console.WriteLine(k);

k = s.ToLower();
Console.WriteLine(k);
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public string Substring(int startIndex)
public string Substring(int startIndex, int length)

*/
namespace Nesne
{

class App
{
public static void Main()
{
string s, k;
s = "istanbul";
k = s.Substring(2); // "tanbul"
Console.WriteLine(k);
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public string Substring(int startIndex)
public string Substring(int startIndex, int length)

*/
namespace Nesne
{

class App
{
public static void Main()
{
string s, k;
s = "istanbul";
k = s.Substring(2, 3); // "tan"
Console.WriteLine(k);
}
}
}
/////////////////////////
////////////////////////////
using System.Net.Http.Headers;
/*
String Sınıfı
public string Substring(int startIndex)
public string Substring(int startIndex, int length)
*/
namespace Nesne
{

class App
{
public static void Main()
{
string date;
string day, month, year;

Console.Write("gg/aa/yyyy formatında tarih giriniz : ");


date = Console.ReadLine();

// gg-aa-yyyy
day = date.Substring(0, 2);
month = date.Substring(3, 2);
year = date.Substring(6, 4);

Console.WriteLine("{0}-{1}-{2}", day, month, year);


}
}
}
/////////////////////////
////////////////////////////
using System.Net.Http.Headers;
/*
String Sınıfı
public int IndexOf(char value)
public int IndexOf(string value)
public int IndexOf(char value, int startIndex)
public int IndexOf(string value, int startIndex)
*/
namespace Nesne
{

class App
{
public static void Main()
{
string s;
int index;

s = "Bursa Uludag Universitesi";


index = s.IndexOf('i');
// karakteri bulduğunda indis numarası ile döner
// karakteri bulamazsa -1 değeri ile döner

if (index == -1)
Console.WriteLine("Cannot find!!!");
else
Console.WriteLine("Found at the {0} index", index);

}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public int IndexOf(char value)
public int IndexOf(string value)
public int IndexOf(char value, int startIndex)
public int IndexOf(string value, int startIndex)
*/
namespace Nesne
{

class App
{
public static void Main()
{
string text;
int indexBeg, indexEnd;
// Bugün {hava} çok sıcak

Console.Write("Bir yazı giriniz : ");


text = Console.ReadLine();
// hava
if ((indexBeg = text.IndexOf('{')) == -1)
{
Console.WriteLine("Yanlış giriş");
return;
}
if ((indexEnd = text.IndexOf('}')) == -1)
{
Console.WriteLine("Yanlış giriş");
return;
}

if(indexBeg > indexEnd)


{
Console.WriteLine("Yanlış giriş");
return;
}
string result = text.Substring(indexBeg + 1, indexEnd - indexBeg - 1);
Console.WriteLine(result);
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public int IndexOf(char value)
public int IndexOf(string value)
public int IndexOf(char value, int startIndex)
public int IndexOf(string value, int startIndex)
*/
namespace Nesne
{
class App
{
public static void Main()
{
string text, result;
int beg = 0, end = 0;
// Bugün {hava} çok sıcak

Console.Write("Bir yazı giriniz : ");


text = Console.ReadLine();
// hava
if ((beg = text.IndexOf('{')) == -1)
Console.WriteLine("Yazının formatı bozuk");
else if ((end = text.IndexOf('}')) == -1)
Console.WriteLine("Yazının formatı bozuk");
else if (beg > end)
Console.WriteLine("Yazının formatı bozuk");
else
{
result = text.Substring(beg + 1, end - beg - 1);
Console.WriteLine(result);
}
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public int IndexOf(char value)
public int IndexOf(string value)
public int IndexOf(char value, int startIndex)
public int IndexOf(string value, int startIndex)
*/
namespace Nesne
{

class App
{
public static void Main()
{
string text, result;
int beg = 0, end = 0;
// Bugün {hava} çok sıcak

Console.Write("Bir yazı giriniz : ");


text = Console.ReadLine();
beg = text.IndexOf('{');
end = text.IndexOf('}');

if (beg == -1 || end == -1 || beg > end)


Console.WriteLine("Yazının formatı bozuk");
else
{
result = text.Substring(beg + 1, end - beg - 1);
Console.WriteLine(result);
}
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public int LastIndexOf(char value)
public int LastIndexOf(string value)
public int LastIndexOf(char value, int startIndex)
public int LastIndexOf(string value, int startIndex)
*/
namespace Nesne
{

class App
{
public static void Main()
{
string path, fileName;
int index;

// c:\windows\temp\a.dat
// Yol ifadesindeki dosyanın ismi yazdırılacak.
// Yani sadece 'a' yazdırılacak
/*
bursa.uludag.docx
Dosya isminde birden fazla '.' karakteri bulunabilir.
Bu durumda uzantı son '.' karakterinin sağındaki karakterlerden
oluşmaktadır
*/

Console.Write("Bir yol ifadesi giriniz : ");


path = Console.ReadLine();

if((index = path.LastIndexOf('\\')) == -1)


{
Console.WriteLine("Girilen yol ifadesi geçerli değil");
return;
}
fileName = path.Substring(index + 1);
if((index = fileName.LastIndexOf('.')) != -1)
fileName = fileName.Substring(0, index);

Console.WriteLine(fileName);

}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public string Remove(int startIndex)
public string Remove(int startIndex, int count)

>>> Remove metodu silinmiş yazı ile geri döner


*/
namespace Nesne
{

class App
{
public static void Main()
{
string text, s;
text = "Bursa Uludag Universitesi";

//s = text.Remove(5);
//Console.WriteLine(s); // Bursa

s = text.Remove(6, 6);
Console.WriteLine(s); // "Bursa Universitesi"
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
public string Insert(int startIndex, string value)

>>> Bir yazıya başka bir yazıyı insert etmek için kullanılır
*/
namespace Nesne
{

class App
{
public static void Main()
{
string text, s;
text = "isbul";

s = text.Insert(2, "tan");
Console.WriteLine(s); // istanbul
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
Trim
>> yazının başındaki ve sonundaki fazladan boşlukları kaldırır
*/
namespace Nesne
{

class App
{
public static void Main()
{
string text, s;
text = " Uludag universitesi ";

s = text.Trim();
Console.WriteLine(":{0}:", s);
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
Replace
public string Replace(char oldChar, char newChar)
public string Replace(string oldValue, string newValue)

*/
namespace Nesne
{

class App
{
public static void Main()
{
string text, s;
text = "Uludag universitesi";

s = text.Replace('e', 'E');
Console.WriteLine("{0}", s);
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
String Sınıfı
Replace
public string Replace(char oldChar, char newChar)
public string Replace(string oldValue, string newValue)

*/
namespace Nesne
{

class App
{
public static void Main()
{
string text, s;
text = "ali top at, ali ip atla";

s = text.Replace("ali", "veli");
Console.WriteLine("{0}", s);
}
}
}
/////////////////////////
////////////////////////////

// See https://aka.ms/new-console-template for more information


using System.Net.Http.Headers;
/*
DİZİLER

*/
namespace Nesne
{

class App
{
public static void Main()
{
int a; // a değerin kendisini tutar
int [] b; // b adres tutar
// Diziler C#'ta sınıfsal biçinde temsil edilir
b = new int[100]; // dizinin uzunuluğu belirtilirken tamsayı türüne
ilişkin olmalıdır
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
DİZİLER

*/
namespace Nesne
{

class App
{
public static void Main()
{
int[] a = new int[100];
for (int i = 0; i < a.Length; i++)
a[i] = i * i;
for (int i = 0;i < a.Length; i++)
Console.Write("{0} ", a[i]);

Console.WriteLine();
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
DİZİLER

*/
namespace Nesne
{

class App
{
public static void Main()
{
int[] a = new int[5] {1, 2, 3, 4 , 5}; // ilk değer verilebilir
int[] b = new int[] { 1, 2, 3, 4, 5 }; // ilk değer verilebilir
int[] c = new int[4] { 1, 2, 5 }; // Error!!!
int[] d;
int n = 3;
d = new int[n] { 1, 2, 3 }; // Error!!!
// ilk değer verildiğinde, uzunluk belirten ifadenin tam sayı olması
gerekir

}
} /////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
DİZİLER

*/
namespace Nesne
{

class App
{
public static void Main()
{
int[] a = new int[] { 8, 12, 52, 17, 56, 9, 125, 13, 162, 84 };
int max = a[0];

for (int i = 1; i < a.Length; i++)


if (max < a[i])
max = a[i];

Console.WriteLine("En Büyük : {0}", max);


}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
DİZİLER
Bir metodun parametre değişkeni dizi türden olabilir

*/
namespace Nesne
{

class App
{
public static void Main()
{
int[] a = new int[] { 8, 12, 52, 17, 56, 9, 125, 13, 162, 84 };
int max;
MyArray.Disp(a);
max = MyArray.GetMax(a);
Console.WriteLine("En Büyük : {0}", max);
}
}
class MyArray
{
public static int GetMax(int[] a)
{
int max = a[0];

for (int i = 1; i < a.Length; i++)


if (max < a[i])
max = a[i];
return max;
}
public static void Disp(int[] a)
{
for (int i = 0; i < a.Length; i++)
Console.Write("{0} ", a[i]);
Console.WriteLine();
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
DİZİLER
Bir metodun parametre değişkeni dizi türden olabilir

*/
namespace Nesne
{

class App
{
public static void Main()
{
int[] a = new int[] { 8, 12, 52, 17, 56, 9, 125, 13, 162, 84 };

Sort.Disp(a);
Sort.Bubble(a);
Sort.Disp(a);
}
}
class Sort
{
public static void Bubble(int[] a)
{
for (int i = 0; i < a.Length - 1; i++)
for (int k = 0; k < a.Length - 1; k++)
if (a[k] > a[k + 1])
{
int temp = a[k];
a[k] = a[k + 1];
a[k + 1] = temp;
}
}
public static void Disp(int[] a)
{
for (int i = 0; i < a.Length; i++)
Console.Write("{0} ", a[i]);
Console.WriteLine();
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
DİZİLER
Bir metodun parametre değişkeni dizi türden olabilir

*/
namespace Nesne
{

class App
{
public static void Main()
{
int[] a = new int[] { 8, 12, 52, 17, 56, 9, 125, 13, 162, 84 };

Sort.Disp(a);
//Sort.Bubble(a);
Sort.Selection(a);
Sort.Disp(a);
}
}
class Sort
{
public static void Bubble(int[] a)
{
for (int i = 0; i < a.Length - 1; i++)
for (int k = 0; k < a.Length - 1; k++)
if (a[k] > a[k + 1])
{
int temp = a[k];
a[k] = a[k + 1];
a[k + 1] = temp;
}
}
public static void Selection(int[] a)
{
int min, minIndex;
for (int i = 0; i < a.Length - 1; i++)
{
min = a[i];
minIndex = i;
for (int k = i + 1; k < a.Length; k++)
if (a[k] < min)
{
min = a[k];
minIndex = k;
}
a[minIndex] = a[i];
a[i] = min;
}
}
public static void Disp(int[] a)
{
for (int i = 0; i < a.Length; i++)
Console.Write("{0} ", a[i]);
Console.WriteLine();
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
DİZİLER
Bir metodun parametre değişkeni dizi türden olabilir

*/
namespace Nesne
{

class App
{
public static void Main()
{
int[] a = new int[] { 8, 12, 52, 17, 56, 9, 125, 13, 162, 84 };

MyArray.Disp(a);
Array.Sort(a); // diziyi sıralar
Array.Reverse(a); // Diziyi tersyüz eder
MyArray.Disp(a);
}
}
class MyArray
{
public static void Disp(int[] a)
{
for (int i = 0; i < a.Length; i++)
Console.Write("{0} ", a[i]);
Console.WriteLine();
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
DİZİLER
Bir metodun parametre değişkeni dizi türden olabilir

*/
namespace Nesne
{

class App
{
public static void Main()
{
int[] a = new int[] { 8, 12, 52, 17, 56, 9, 125, 13, 162, 84 };
int index;

MyArray.Disp(a);
index = Array.IndexOf(a, 56);

if (index == -1)
Console.WriteLine("Bulunamadı!");
else
Console.WriteLine("Bulundu: {0}", index);
}
}
class MyArray
{
public static void Disp(int[] a)
{
for (int i = 0; i < a.Length; i++)
Console.Write("{0} ", a[i]);
Console.WriteLine();
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
DİZİLER
Bir metodun geri dönüş değeri dizi türden olabilir

*/
namespace Nesne
{

class App
{
public static void Main()
{
int[] a;
a = Sample.GetRandomNumbers(1, 100, 10);

Sample.Disp(a);
}
}
class Sample
{
public static int[] GetRandomNumbers(int min, int max, int n)
{
Random rand = new Random();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = rand.Next(min, max+ 1);
return a;
}
public static void Disp(int[] a)
{
for (int i = 0; i < a.Length; i++)
Console.Write("{0} ", a[i]);
Console.WriteLine();
}
}
}
/////////////////////////
////////////////////////////
// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;
/*
DİZİLER
Bir metodun geri dönüş değeri dizi türden olabilir

*/
namespace Nesne
{

class App
{
public static void Main()
{
Lotto lotto = new Lotto();
int[] column;
column = lotto.GetRandomColumn();

Array.Sort(column);
Lotto.Disp(column);
}
}
class Lotto
{
public Random rand;
public Lotto()
{
rand = new Random();
}
public int[] GetRandomColumn()
{
int[] column = new int[6];
int val;
bool repeat;

for (int k = 0; k < 6; k++)


{
do
{
repeat = false;
val = rand.Next(1, 50);
for (int i = 0; i < k; ++i)
if (column[i] == val)
{
repeat = true;
break;
}
} while (repeat);
column[k] = val;
}
return column;
}
public static void Disp(int[] a)
{
for (int i = 0; i < a.Length; i++)
Console.Write("{0} ", a[i]);
Console.WriteLine();
}
}
}
/////////////////////////
////////////////////////////

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy