簡體   English   中英

如何從長度未知的文本框中的文本中獲取每兩個字符的 substring

[英]How can I get the substring of every two characters from text in a textbox with an unknown length

我正在開發一個使用四個字節的十六進制代碼(例如 12 12 12 12)生成代碼的輔助項目,我得到了生成它的部分,但我希望能夠將它與任何長度的十六進制代碼一起使用(例如 12 12 12 12 12 12 12 12 12 12,基本上任何長度)我不知道如何,這就是我現在所擁有的

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

namespace CodeGenerator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void copyoutputBytesbtn_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(outputbytesTxtbox.Text);
        }

        private void copyfullCodebtn_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(fullcodeTxtbox.Text);
        }

        private void inputBytestxtbox_TextChanged(object sender, EventArgs e)
        {
            if (inputBytestxtbox.Text.Contains(" "))
            {
                string inputBytes = inputBytestxtbox.Text.Replace(" ", String.Empty);
                string f1 = inputBytes.Substring(0, 2);
                string f2 = inputBytes.Substring(2, 2);
                string f3 = inputBytes.Substring(4, 2);
                string f4 = inputBytes.Substring(6, 2);
                outputbytesTxtbox.Text = "0x" + f1 + " 0x" + f2 + " 0x" + f3 + " 0x" + f4;
                //original format: 12 12 12 12//
                //output format: 0x12 0x12 0x12 0x12//
            }
        }
    }
}

我希望能夠使用任何長度的十六進制代碼而不僅僅是四個,無論如何我可以 go 這樣做嗎?

您可以使用for循環:

static void Main(string[] args)
{
    string input = "121212121212121"; // Your input form textblock
    string[] result = SplitByTwo(input); // splitting the string
    foreach (string s in result) // showing results
        Console.Write($"{s} ");
}

public static string[] SplitByTwo(string input)
{
    // the + 1 is here in case that the length is not divisible by 2
    // examle: if length is 6 the array kength must be 3 | (6 + 1) / 2 = 3 (integer division)
    //         if length is 7 the array length must be 4 | (7 + 1) / 2 = 4
    string[] output = new string[(input.Length + 1) / 2];
    for (int i = 0; i < (input.Length + 1) / 2; i++)
    {
        if ((i * 2) + 1 == input.Length) // in case that the last split only contains 1 character
            output[i] = input.Substring(i * 2, 1);
        else
            output[i] = input.Substring(i * 2, 2);
    }
    return output;
}

output:

12 12 12 12 12 12 12 1

編輯:

如果您的輸入有空格,您可以簡單地拆分字符串,然后將其加入:

string input = "12 12 12 12 12 12 12 1";
string output = "0x" + string.Join(" 0x", input.Split(' '));
Console.WriteLine(output);

Output:

0x12 0x12 0x12 0x12 0x12 0x12 0x12 0x1

請嘗試下面的代碼,如果它工作正常,請給我們反饋:

private void inputBytestxtbox_TextChanged(object sender, EventArgs e)
{
            if (inputBytestxtbox.Text.Contains(" "))
            {
                const int step = 2;
                string inputBytes = inputBytestxtbox.Text.Replace(" ", String.Empty);
                StringBuilder sb = new StringBuilder();
                for(int i = 0; i < inputBytes.Length; i+=step)
                {
                   string fx = inputBytes.Substring(i, step);
                   sb.Append("0x");
                   sb.Append(fx);
                   if(i <= inputBytes.Length - step)
                   {
                      sb.Append(" ");
                   }
                }
                outputbytesTxtbox.Text = sb.ToString();
            }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM