使用索引位置拆分richtextbox文本 [英] Split richtextbox text with index position

查看:126
本文介绍了使用索引位置拆分richtextbox文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在richtextbox中有数据。

i have data in richtextbox.

11 2 3 4 12 23 456 234



i想要每个符号着色。我想要拆分字符串并在类元素列表中添加字符串开头和结尾的索引。


i want colorize every symbol. I want split string and add index of begin and end of string in the List of class Elements.

class Elements
   {
       private string _data;
       private string _begin;
       private string _end;

       public string Data { get; set; }
       public string Begin{ get; set; }
       public string End  { get; set; }
}




[11,0,2]  11 is the first element, 0 begin and 1 end of this string
[2,4,5]   2 is the second element, 4 begin and 5 end of this string.





并使用此代码着色元素



and with this code colorize elements

foreach (var item in List<Element>) // example code
            richTextBox1.SelectionStart = item.Begin;
            richTextBox1.SelectionLength = item.End
            richTextBox1.SelectionColor = Color.DarkBlue;



如何在类元素中插入数据。我不明白如何定义元素的开始和结束?


How insert data in Class Elements. I dont understand how define begin and ending of elements?

推荐答案

下面的代码是一种方法,但我不确定我是否完全清楚您的需求。下面我得到值,然后是该值的开始和结束索引。所以它将是[11,0,1],[2,3,3],...,因为这是值的实际开始和结束索引。你有什么指数?如果在令牌结束后获得空白字符的索引,则在调用Element构造函数时将减法除去1。哦,我使用的是结构而不是类,因为它使用堆栈而不是堆更快。



The below code is one way to do it, but I'm not sure I'm totally clear on your needs. Below I get the value, and then the begin and end indices of that value. So it would be [11,0,1], [2,3,3], ..., because that is the actual start and end index of the value. What are you getting the indices of? If you are getting the index of the whitespace character after the end of the token, then remove the subtraction by one when calling the Element constructor. Oh, and I used a Struct rather than Class because it's a bunch faster using the stack instead of the heap.

 string values = "11 2 3 4 12 23 456 234";
 List<Element> elements = GetElements(values);
 // Render to RichTextBox

// Precondition: All values are single whitespace delimited
public static List<Element> GetElements(string input) {
    List<Element> elements = new List<Element>();
    string value = "";
    for (int beginI = 0, i = 0; i < input.Length; i++)
    {
        if (input[i] == ' ') // end of token
        {
            elements.Add(new Element(value, beginI, i - 1));
            value = "";
            beginI = i + 1;
            continue;
        }
        else if(i == input.Length - 1) // end of input string
            elements.Add(new Element(value +  input[i], beginI, i - 1));
        else
            value += input[i];
    }
    return elements;
}

public struct Element
{
    public string Data;
    public int End;
    public int Begin;
    public Element (string data, int begin, int end){
        Data = data;
        Begin = begin;
        End = end;
    }
}


我认为你可能比想象的更接近为此寻找解决方案。



此处显示的代码假设:每个数据项通过一个空格字符与其他数据项分开。即,在被解析的数据中没有换行符,没有标签等。要处理由一些任意数量的空白分隔的数据项,需要采用不同的方法,但可以完成。



这里的代码有一个最重要的步骤让你弄清楚
I think you may be closer than you think you are to finding a solution for this.

The code shown here assumes: Each data-item is separated from the other data-items by one space character. i.e., there are no line-breaks in the data being parsed, no tabs, etc. To handle data-items separated by some arbitrary amount of white-space would require a different approach, but could be done.

The code here has the single most important step left for you to figure out.
private class Elements
{
   public string eData { get; set; }
   public int eStart { get; set; }
   public int eLength { get; set; }

   public Elements(string data, int start, int length)
   {
       eData = data;
       eStart = start;
       eLength = length;
   }
}

// you want to store the results of your parsing the data
private List<elements> theElements = new List<elements>();

// for splitting the data string:
// assumes each data item is separated by one space from other data items
private char[] splitChar = new[] { ' ' };

// some test data: assume this is on one line in a RichTextBox
// named 'richTextBox1
private string testData = "11 2 3 4 12 23 456 234";

private void createElements(string data)
{
    theElements.Clear();

    string[] entries = data.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);

    int begin = 0;

    for (int i = 0; i < entries.Length; i++)
    {
        string entry = entries[i];
        int eLen = entry.Length;

        theElements.Add(new Elements(entry, begin, eLen));

        begin += // for you to figure out
    }


    for (int i = 0; i < theElements.Count; i++)
    {
        var element = theElements[i];

        richTextBox1.Select(element.eStart, element.eLength);

        // alternate the colors of the items as a
        // visual test of the code
        richTextBox1.SelectionColor = (i%2 == 0)
            ? Color.Red
            : Color.Blue;
    }
}


这篇关于使用索引位置拆分richtextbox文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆