Vous êtes sur la page 1sur 19

Stringovi, nizovi i

kolekcije
Pregled
Stringovi
.NET Framework Nizovi
.NET Framework kolekcije
Stringovi
Parse
Formatiranje
Velika i mala slova
Compare
Trim i Pad
Split i Join
StringBuilder
C# specifinosti
Parse
Metoda Parse konvertuje numeriki string u broj
string
string MyString
MyString == "12345";
"12345";
int
int MyInt
MyInt == int.Parse(MyString);
int.Parse(MyString);
MyInt++;
MyInt++;
Console.WriteLine(MyInt);
Console.WriteLine(MyInt);
//
// The
The output
output to
to the
the console
console is
is "12346".
"12346".

Da bi se ignorisale zapete koristi se fleg


NumberStyles.AllowThousands
string
string MyString
MyString == "123,456";
"123,456";
int
int MyInt
MyInt == int.Parse(MyString,
int.Parse(MyString,
System.Globalization.NumberStyles.AllowThousands);
System.Globalization.NumberStyles.AllowThousands);
Console.WriteLine(MyInt);
Console.WriteLine(MyInt);
//
// The
The output
output to
to the
the console
console is
is "123456".
"123456".
Primeri formatiranja
Currency format predstavljanje novanih
vrednosti
C - $XX,XXX.XX

int
int MyInt
MyInt == 12345;
12345;
string
string MyString
MyString == MyInt.ToString("C"
MyInt.ToString("C" ););
//
// In
In the
the U.S.
U.S. English
English culture:
culture: "$12,345.00"
"$12,345.00"
Format za datum i vreme
D - dd MMMM yyyy

d - MM/dd/yyyy

DateTime
DateTime MyDate
MyDate == new
new DateTime(2000,
DateTime(2000, 1,
1, 10,
10, 0,
0, 0,
0, 0);
0);
string
string MyString
MyString == MyDate.ToString(
MyDate.ToString( "d"
"d" ););
// In the U.S. English culture: "1/10/2000"
// In the U.S. English culture: "1/10/2000"
Velika i mala slova

String.ToUpper konvertuje sva slova u


velika
string
string MyString
MyString == "hello
"hello world!";
world!";
//
// outputs:
outputs: HELLO
HELLO WORLD!
WORLD!
Console.WriteLine(MyString.ToUpper());
Console.WriteLine(MyString.ToUpper());
String.ToLower konvertuje sva slova u
mala
string
string MyString
MyString == "HELLO
"HELLO WORLD!";
WORLD!";
//
// outputs:
outputs: hello
hello world!
world!
Console.WriteLine(MyString.ToLower());
Console.WriteLine(MyString.ToLower());
Compare
Statika metoda za poreenje stringova
Na primer, Compare metod poredi dva
objekta klase string i vraa:
- negativan broj ako je prvi string manji

- 0 ako su stringovi jednaki

- pozitivan broj ako je prvi string vei od

drugog
string
string MyString
MyString == "Hello
"Hello World!";
World!";
Console.WriteLine(
Console.WriteLine(
String.Compare(MyString,"Hello
String.Compare(MyString,"Hello World!"));
World!"));
//
// outputs:
outputs: 00
Metode Trim i Pad
Trim metoda izbacuje prazna mesta
string
string MyString
MyString == "" Big
Big ";
";
Console.WriteLine("Hello{0}World!",
Console.WriteLine("Hello{0}World!", MyString
MyString );
);
string
string TrimString
TrimString == MyString.Trim();
MyString.Trim();
Console.WriteLine("Hello{0}World!",
Console.WriteLine("Hello{0}World!", TrimString
TrimString ););
//
// outputs
outputs the
the following
following lines
lines to
to the
the console:
console:
//Hello
//Hello Big
Big World!
World!
//HelloBigWorld!
//HelloBigWorld!
Pad metoda multiplicira karakter odreeni broj
puta
string
string MyString
MyString == "Hello
"Hello World!";
World!";
Console.WriteLine(MyString.PadLeft(20,
Console.WriteLine(MyString.PadLeft(20, '-'));
'-'));
//
// outputs
outputs the
the following
following line
line to
to the
the console:
console:
//--------Hello
//--------Hello World!
World! to
to the
the console.
console.
Metode Split i Join
Split metoda deli string u niz stringova
String se see na mestima na kojima se nalazi
prosleeni separator
Ako je separator vrednost null onda se blanko
karakteri proglaavaju za separatore
string
string Line
Line == "Hello
"Hello World";
World";
string[]
string[] Words
Words == Line.Split(null);
Line.Split(null);
//
// Words[0]
Words[0] == "Hello"
"Hello" and
and Words[1]
Words[1] == "World"
"World"

Join metoda slui za konkatenaciju stringova


Umee prosleeni separator izmeu elemenata
prosleenog niza stringova
Klasa StringBuilder
Objekat klase string je nepromenljiv
System.Text.StringBuilder omoguava
promenu stringa bez kreiranja novog objekta
StringBuilder
StringBuilder MyStringBuilder
MyStringBuilder == new
new StringBuilder("Hello");
StringBuilder("Hello");

Moe se specificirati maksimalan broj karaktera


//
// MyStringBuilder
MyStringBuilder can
can hold
hold aa maximum
maximum of
of 25
25 characters
characters
StringBuilder
StringBuilder MyStringBuilder
MyStringBuilder ==
new
new StringBuilder("Hello
StringBuilder("Hello World!",
World!", 25);
25);

Poseduje metode:
Append, AppendFormat, Insert, Remove,
i Replace
Specifinosti jezika C#
C# string je niz Unicode karaktera
String je alias za System.String
Operatori == i != uporeuju vrednosti, a ne reference
string objekata
+ operator slui za konkatenaciju stringova
string
string aa == "\u0068ello
"\u0068ello ";
";
string
string bb == "world";
"world";
Console.WriteLine(
Console.WriteLine( aa ++ bb ==
== "hello
"hello world"
world" );//True
);//True
[ ] operator omoguuje pristup pojedinim
karakterima u stringu
char
char xx == "test"[2];
"test"[2]; //
// xx == 's';
's';

@-quoting slui da se izbegnu tzv. escape


sekvence
@"c:\Docs\Source\a.txt"
@"c:\Docs\Source\a.txt"
//
// rather
rather than
than "c:\\Docs\\Source\\a.txt"
"c:\\Docs\\Source\\a.txt"
.NET Framework nizovi
System.Array
Specifinostijezika C#
Pregled lanova niza Iterating Over
Poreenje
Sortiranje
Klasa System.Array
System.Array je bazna klasa za sve nizove
Nizovi implementiraju sledee interfejse
ICloneable, IList, ICollection i IEnumerable

System.Array ima metode za:


Kreiranje, manipulaciju, pretraivanje i
sortiranje nizova
Vrednost Null, prazan string i prazan niz se
tretiraju na isti nain
Specifinosti jezika C#
Indeksi u nizovima poinju od 0
Deklarisanje niza, bez odreivanja veliine
int[]
int[] numbers;
numbers; //
// declare
declare numbers
numbers as
as
//
// an
an int
int array
array of
of any
any size
size

Kreiranje niza
int[]
int[] numbers
numbers == new
new int[5];
int[5]; //
// declare
declare and
and create
create

Inicijalizacija niza
int[]
int[] numbers
numbers == new
new int[5]
int[5] {1,
{1, 2,
2, 3,
3, 4,
4, 5};
5};
Korienje lanova klase System.Array
int[]
int[] numbers
numbers == {1,
{1, 2,
2, 3,
3, 4,
4, 5};
5};
int
int LengthOfNumbers
LengthOfNumbers == numbers.Length;
numbers.Length;
Iterating Over
Klase System.Array i System.Collections
Classes implementiraju interfejs IEnumerable i
njegovu metodu GetEnumerator
lanovi interfesa IEnumerator su:
MoveNext, Reset i Current

int[]
int[] numbers
numbers == new
new int[5]
int[5] {1,
{1, 2,
2, 3,
3, 4,
4, 5};
5};
IEnumerator
IEnumerator ee == numbers.GetEnumerator();
numbers.GetEnumerator();
while
while (e.MoveNext())
(e.MoveNext()) {{
Console.WriteLine("Number:
Console.WriteLine("Number: {0}", {0}", (int)e.Current);
(int)e.Current);
}}
//
// alternatively
alternatively
foreach
foreach (int
(int ii in
in numbers)
numbers)
{{
Console.WriteLine("Number:
Console.WriteLine("Number: {0}", {0}", i);
i);
}}
Poreenje
Za operacije Sort i Search, nizovi moraju biti u stanju da porede lanove.
Metod Compare interfejsa IComparer koji poredi dva objekta bilo kog tipa
Klasa Comparer je osnovna implementacija interfejsa IComparer
Njena Compare metoda koristi IComparable.CompareTo
int
int CompareTo(
CompareTo( object
object anObject
anObject );
);
int
int Compare(
Compare( object
object x,
x, object
object yy );
);
CompareTo vraa

Value Meaning
Less than zero Instance is less than object
Zero Instance is equal to object
Greater than zero Instance is greater than object
Sortiranje
Sort metod koristi IComparable.CompareTo
Array.Sort(
Array.Sort( anArray
anArray );
);

IComparable.CompareTo Design Pattern


public
public int
int CompareTo(Object
CompareTo(Object anObject)
anObject) {{
if
if (( anObject
anObject ==== null)
null) return
return 1;
1;
if
if (( !(anObject
!(anObject isis <classname>)
<classname>) )) {{
throw
throw new
new ArgumentException();
ArgumentException(); }}
//
// Do
Do comparison
comparison and
and return
return aa
//
// negative
negative integer
integer if
if instance
instance << anObject
anObject
//
// 00 ifif instance
instance ==
== anObject
anObject
//
// positive
positive integer
integer if
if instance
instance >> anObject
anObject
}}
.NET Framework
Klase iz System.Collections
Liste
Renici
Klasa SortedList
Preporuke
Type Safety i Performance
Klase iz System.Collections
ArrayList
Implementira dinamiki niz promenljive
veliine implementirajui IList
DictionaryBase
Apstraktna klasa za sve klase koje

implementiraju key value klase


Hashtable
SortedList
BitArray, Queue, Stack, CollectionBase,
ReadOnlyCollectionBase

Vous aimerez peut-être aussi