تبلیغات
مرجع عملی و کاربردی زبان سی شارپ ، تصاویر دیدنی ، دانلود نرم افزار های گوناگون - کار کردن با انواع داده ها

کار کردن با انواع داده ها

نویسنده :مرتضی عالمین
تاریخ:دوشنبه 11 خرداد 1388-01:25 ب.ظ

2-رشته ها

2-1-استفاده از Place Holder ها در کار با رشته ها


فرض کنیم که :

کد:

int a = 3, b = 5;

حال میتونیم به جای نوشتن :

کد:

string result = a.ToString() + "+" + b.ToString() + "=" + (a + b).ToString();

بسادگی بنویسیم :

کد:

string result = string.Format("{0}+{1}={2}", a, b, a + b);

استفاده از Place Holder ها (حالت دوم) ضمن خوانایی بیشتر برنامه، معمولا کدنویسی کمتری نیز طلب میکند.

2-2-خوندن خط به خط یک رشته چند سطری


کد:

using System.IO;

کد:

string s = @"salam

    dooste

    man";

using (StringReader sr = new StringReader(s))

{

    while (sr.Peek() > 0)

        MessageBox.Show(sr.ReadLine());

};

2-3-حالتهای مختلف نمایش تاریخ و زمان با کمک String Formatting


کد:

static void Main(string[] args)
{
    DateTime CurrTime = DateTime.Now;
    Console.WriteLine("DateTime display listing specifier and result:\n");
    Console.WriteLine("d = {0:d}", CurrTime); 
        // Short date mm/dd/yyyy
    Console.WriteLine("D = {0:D}", CurrTime); 
        // Long date day, month dd, yyyy
    Console.WriteLine("f = {0:f}", CurrTime); 
        // Full date/short time day, month dd, yyyy hh:mm
    Console.WriteLine("F = {0:F}", CurrTime); 
        // Full date/full time day, month dd, yyyy HH:mm:ss AM/PM
    Console.WriteLine("g = {0:g}", CurrTime); 
        // Short date/short time mm/dd/yyyy HH:mm
    Console.WriteLine("G = {0:G}", CurrTime); 
        // Short date/long time mm/dd/yyyy hh:mm:ss
    Console.WriteLine("M = {0:M}", CurrTime); 
        // Month dd
    Console.WriteLine("R = {0:R}", CurrTime); 
        // ddd Month yyyy hh:mm:ss GMT
    Console.WriteLine("s = {0:s}", CurrTime); 
        // yyyy-mm-dd hh:mm:ss can be sorted!
    Console.WriteLine("t = {0:t}", CurrTime); 
        // Short time hh:mm AM/PM
    Console.WriteLine("T = {0:T}", CurrTime); 
        // Long time hh:mm:ss AM/PM
    Console.WriteLine("u = {0:u}", CurrTime); 
        // yyyy-mm-dd hh:mm:ss universal/sortable
    Console.WriteLine("U = {0:U}", CurrTime); 
        // day, month dd, yyyy hh:mm:ss AM/PM
    Console.WriteLine("Y = {0:Y}", CurrTime); 
        // Month, yyyy
    Console.WriteLine();
    Console.WriteLine("DateTime.Month = " + CurrTime.Month); 
        // number of month
    Console.WriteLine("DateTime.DayOfWeek = " + CurrTime.DayOfWeek); 
        // full name of day
    Console.WriteLine("DateTime.TimeOfDay = " + CurrTime.TimeOfDay); 
    Console.Read();     // wait
}

منبع : http://www.daniweb.com/code/snippet184.html
یک لینک مفید دیگر : http://blog.stevex.net/index.php/str...ing-in-csharp/

2-4-تبدیل آرایه از بایتها به یک رشته و بلعکس


کد:

//You have a byte[] representing some binary information, such as a bitmap.

// You need to encode this data into a string so that it can be sent over

// a binary-unfriendly transport, such as email.

public string Base64EncodeBytes(byte[] inputBytes)

{

    return (Convert.ToBase64String(inputBytes));

}

کد:

//You have a String that containsinformation such asa bitmap encoded

// asbas e64. You need to decode this data (which may have been embedded in an

// email message) from a String into a byte[] so that you can access

// the original binary.

public byte[] Base64DecodeString(string inputStr)

{

    byte[] decodedByteArray = Convert.FromBase64String(inputStr);

    return (decodedByteArray);

}

2-5-بدست آوردن حروف تشکیل دهنده ی یک رشته


کد:

string testStr = "Sinpin";

foreach (char c in testStr)

    MessageBox.Show(c.ToString());

کد:

string testStr = "Sinpin";

for (int counter = 0; counter < testStr.Length; counter++)

    MessageBox.Show(testStr[counter].ToString());

و البته روش اول بهینه تر است.

2-6-هرس کردن یک رشته متنی


حذف حروف خاص

You have a string with a specific set of characters, such as spaces, tabs, escaped single double quotes, any type of punctuation character(s), or some other character(s), at the beginning and/or end of a string. You want a simple way to remove these characters.

کد:

private void PruningCharacters()

{

    string foo = "--TEST--";

    Console.WriteLine(foo.Trim(new char[] {'-'})); // Displays "TEST"

    foo = ",-TEST-,-";

    Console.WriteLine(foo.Trim(new char[] {'-',','})); // Displays "TEST"

    foo = "--TEST--";

    Console.WriteLine(foo.TrimStart(new char[] {'-'})); // Displays "TEST--"

    foo = ",-TEST-,-";

    Console.WriteLine(foo.TrimStart(new char[] {'-',','})); // Displays "TEST-,-"

    foo = "--TEST--";

    Console.WriteLine(foo.TrimEnd(new char[] {'-'})); // Displays "--TEST"

    foo = ",-TEST-,-";

    Console.WriteLine(foo.TrimEnd(new char[] {'-',','})); //Displays ",-TEST"

}

 

 




داغ کن - کلوب دات کام
نظرات() 
How do you get taller in a day?
شنبه 1 مهر 1396 06:58 ب.ظ
Heya i'm for the primary time here. I came across this board and I to
find It really helpful & it helped me out much. I'm hoping to provide something back and aid others like you helped me.
What is distraction osteogenesis?
سه شنبه 28 شهریور 1396 04:28 ق.ظ
Thanks for sharing your thoughts about Can Pilates make you
look taller?. Regards
foot pain
دوشنبه 13 شهریور 1396 11:50 ق.ظ
Wonderful blog! I found it while surfing around on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I've been trying for a while but I never seem to get there!

Thank you
How do you treat Achilles tendonitis?
دوشنبه 16 مرداد 1396 05:32 ب.ظ
Hello! I know this is kinda off topic however , I'd figured I'd ask.

Would you be interested in trading links or maybe guest authoring a blog post or
vice-versa? My site covers a lot of the same subjects as
yours and I think we could greatly benefit from each other.
If you are interested feel free to shoot me an email.
I look forward to hearing from you! Awesome blog by the way!
http://paulwhmicwfbtd.jimdo.com
شنبه 17 تیر 1396 12:32 ق.ظ
This is really interesting, You are a very skilled blogger.
I've joined your feed and look forward to seeking more of your magnificent post.
Also, I've shared your web site in my social networks!
BHW
دوشنبه 28 فروردین 1396 07:03 ق.ظ
Hi this is kind of of off topic but I was wondering if blogs use
WYSIWYG editors or if you have to manually code with HTML.
I'm starting a blog soon but have no coding expertise so I wanted to get advice from someone with experience.
Any help would be enormously appreciated!
manicure
سه شنبه 22 فروردین 1396 03:27 ق.ظ
Thank you for the auspicious writeup. It in fact was
a amusement account it. Look advanced to far added agreeable from you!

By the way, how could we communicate?
manicure
پنجشنبه 17 فروردین 1396 04:56 ب.ظ
Greetings! This is my first comment here so I just wanted to give
a quick shout out and say I genuinely enjoy reading through your articles.
Can you suggest any other blogs/websites/forums that deal with the
same subjects? Thanks!
 
لبخندناراحتچشمک
نیشخندبغلسوال
قلبخجالتزبان
ماچتعجبعصبانی
عینکشیطانگریه
خندهقهقههخداحافظ
سبزقهرهورا
دستگلتفکر




Admin Logo
themebox Logo