Académique Documents
Professionnel Documents
Culture Documents
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".
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
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"
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';
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 );
);