50٪ تخفیف روی تمام دوره‌ها!
پایان تخفیف تا:
مشاهده دوره‌ها
0

جدا کردن تعداد کاراکترهای یک رشته در سی شارپ

سلام دوستان خسته نباشید ، من یک متغییر در سی شارپ تعریف کرده ام که مقدار آن 50680af8ca54 می باشد و من میخام کاراکتر ها رو با : از هم جدا کنم یعنی به شکل زیر :

5:68:0a:f8:ca:54

ممنون میشم راهنمایی کنید

پرسیده شده در 1395/09/09 توسط

5 پاسخ

0

ممنون از پاسختون . ولی من کد رو به شکل زیر در برنامه م درج کردم اما متاسفانه با پیغام خطا مواجه میشه .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication138
{
    public partial class Form1 : Form
    {
       
        

        public Form1()
        {
            InitializeComponent();
        }
        public static class StringExtensions
        {
            public static IEnumerable<string> SplitByCharacters(this string str, int characters)
            {
                List<string> substrings = new List<string>();

                for (int index = 0; index < str.Length; index += characters)
                {
                    substrings.Add(index + characters > str.Length ? str.Substring(index) : str.Substring(index, characters));
                }

                return substrings;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {

            var result = string.Join(":", "Welcome to ITPro.ir".SplitByCharacters(2));
                     
        }
    }
}

ممنون از پاسختون . ولی من کد رو به شکل زیر  در برنامه م درج کردم اما متاسفانه با پیغام خطا مواجه میشه .
<left>
<c#>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication138
{
    public partial class Form1 : Form
    {
       
        

        public Form1()
        {
            InitializeComponent();
        }
        public static class StringExtensions
        {
            public static IEnumerable<string> SplitByCharacters(this string str, int characters)
            {
                List<string> substrings = new List<string>();

                for (int index = 0; index < str.Length; index += characters)
                {
                    substrings.Add(index + characters > str.Length ? str.Substring(index) : str.Substring(index, characters));
                }

                return substrings;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {

            var result = string.Join(:, Welcome to ITPro.ir.SplitByCharacters(2));
                     
        }
    }
}
<c#>
<left>

||http://programming.tosinso.com/files/get/16c96038-cb1d-49cf-af75-5f0128f58184||
پاسخ در 1395/09/09 توسط
0

بله میخام دو کاراکتر دوکارکتر جدا کنم

پاسخ در 1395/09/09 توسط
0

الان شما دو کاراکتر دو کاراکتر می خوایید جدا کنید یا هر کاراکتر به صورت جداگانه؟

پاسخ در 1395/09/09 توسط
1

شما اول باید یه Extension Method برای جدا سازی رشته بر اساس طول کاراکترها بنویسید:

public static class StringExtensions
{
    public static IEnumerable<string> SplitByCharacters(this string str, int characters)
    {
        List<string> substrings = new List<string>();

        for (int index = 0; index < str.Length; index += characters)
        {
            substrings.Add(index + characters > str.Length ? str.Substring(index) : str.Substring(index, characters));
        }

        return substrings;
    }
}

حالا به صورت زیر می تونید از این متد استفاده کنید، خروجی این متد یک شئ از نوع IEnumerable هست:

var result = "Welcome to ITPro.ir".SplitByCharacters(2);

حالا باید خروجی این متد رو بر اساس کاراکتر مورد نظر به هم متصل کنید:

var result = string.Join(":", "Welcome to ITPro.ir".SplitByCharacters(2));

خروجی کد بالا به صورت زیر هست:

We:lc:om:e :to: I:TP:ro:.i:r
پاسخ در 1395/09/09 توسط
1

کلاس StringExtensions رو خارج از کلاس Form تعریف کنید:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication138
{
    public partial class Form1 : Form
    {
       
        

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            var result = string.Join(":", "Welcome to ITPro.ir".SplitByCharacters(2));
                     
        }
    }

        public static class StringExtensions
        {
            public static IEnumerable<string> SplitByCharacters(this string str, int characters)
            {
                List<string> substrings = new List<string>();

                for (int index = 0; index < str.Length; index += characters)
                {
                    substrings.Add(index + characters > str.Length ? str.Substring(index) : str.Substring(index, characters));
                }

                return substrings;
            }
        }
}
پاسخ در 1395/09/09 توسط

پاسخ شما