Thứ Sáu, 10 tháng 12, 2010

C# String Demo

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//string s1 = "abcd";
//string s2 = "ABCD";
//string s3 = "trung tam tin hoc";
////so sanh phan biet hoa thuong
//int kq = string.Compare(s2, s1);
//Console.WriteLine("Ket qua = {0}", kq);
////So sanh khong phan biet hoa thuong
//kq = string.Compare(s1, s2, true);
//Console.WriteLine("Ket qua = {0}", kq);
////So sanh toan tu
//bool kq1 = (s1 == s2);
//Console.WriteLine("Ket qua = {0}", kq1);
////So sanh bang ham
//bool kq2 = s1.Equals(s2);
//Console.WriteLine("Ket qua = {0}", kq2);
////ghep chuoi
//string s4 = s1 + s2;
//string s5 = string.Concat(s1, s2);
////trich chuoi
//string s6 = "WELCOME TO MICROSOFT VISUAL STUDIO 2005";
//string s7 = s6.Substring(11);
//Console.WriteLine("Ket qua = {0}", s7);
////Nhap ho ten va tach ten
//Console.Write("Nhap ho ten : ");
//s7 = Console.ReadLine();
//string s8 = s7.Substring(s7.LastIndexOf(" "));
//Console.WriteLine("Ten : {0}", s8);
////Xac dinh ky tu dau
//bool kq5 = s6.StartsWith("W");
//Console.WriteLine("Ket qua = {0}", kq5);
////Chen chuoi(vao cuoi)
//string s9 = s6.Insert(s6.Length, " SP2");
//Console.WriteLine("Ket qua = {0}", s9);
////Chen chuoi(vao giua)
//string s10 = s6.Insert(s6.IndexOf("2005"), " VER ");
//Console.WriteLine("Ket qua = {0}", s10);
////Cat chuoi theo dau cach
//string[] s11 = s6.Split(' ');
//foreach(string _s in s11)
// Console.WriteLine(_s);
////Cat chuoi theo ky tu bat ky
//const char khoangtrang = ' ';
//const char dauphay = ',';
//const char dauchamphay = ';';
//char[] c= new char[] {khoangtrang , dauchamphay , dauphay};
//string[] s12=s6.Split(c);
//foreach(string _d in s12)
// Console.WriteLine(_d);
////B1 Viet chuong trinh cho phep nguoi dung nhap vao mot chuoi dem so luong cac ky tu trong chuoi
////B2 Viet chuong trinh nhap vao hai chuoi tim chuoi do co trong chuoi me hay khong neu co cho biet vi tri
////B4 Viet chuong trinh nhap vao mot chuoi cat cac tu trong chuoi
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
Console.WriteLine("B1 Viet chuong trinh cho phep nguoi dung nhap vao mot chuoi dem so luong cac ky tu trong chuoi");
int sl = 0;
char ck;
string kt="abcdefghijklmnoprstuvxytABCDEFGHIJKLMNOPRSTUVXYwWZ1234567890";
Console.Write("Nhap vao chuoi : ");
string intp = Console.ReadLine();
for (int l = 0; l < kt.Length; l++)
{
foreach (char tp in intp)
{
if (kt[l] == tp)
sl++;
ck = kt[l];
}
if (sl != 0)
{
Console.WriteLine("{0} xuat hien trong chuoi la : {1}",kt[l], sl);
sl = 0;
}
}
Console.ReadLine();
//------------------------------------------------------------------------------------
Console.WriteLine("B2 Viet chuong trinh nhap vao hai chuoi tim chuoi do co trong chuoi me hay khong neu co cho biet vi tri");
string in1;
string in2;
int vitri;
Console.Write("Nhap vao chuoi : ");
in1 = Console.ReadLine();
Console.Write("Nhap vao chuoi : ");
in2 = Console.ReadLine();
vitri = in1.LastIndexOf(in2);
Console.WriteLine("Master String : {0}", in1);
Console.WriteLine("Slever String : {0}", in2);
if (vitri > 0)
{
Console.WriteLine("Chuoi \"{0}\" ton tai trong \"{1}\"", in2, in1);
Console.WriteLine("Vi tri : {0}", vitri);
}
else
Console.WriteLine("Chuoi \"{0}\" khong ton tai trong \"{1}\"", in2, in1);
Console.ReadLine();
//-------------------------------------------------------------------------
Console.WriteLine("B3 Viet chuong trinh dao nguoc chuoi");
Console.Write("Nhap vao chuoi : ");
string s_in = Console.ReadLine();
Console.WriteLine("Chuoi nhap vao : {0}", s_in);
char[] s_out = new char[] { };
Console.Write("Chuoi moi tao :");
for (int i = s_in.Length - 1; i >= 0; i--)
Console.Write(s_in[i]);
//---------------------------------------------------------------------------------

//---------------------------------------------------------------------------------
//StringBuilder s = new StringBuilder();
//int i = 1;
//string s1 = "trung tam tin hoc va quan li";
//string[] s2 = s1.Split(' ');
//foreach (string s3 in s2)
// s.AppendFormat("{0}.{1}\n",i++,s3);
//Console.WriteLine(s.ToString());
Console.ReadLine();
}
}
}

Không có nhận xét nào:

Đăng nhận xét