简体   繁体   中英

C# - split a RichTextBox line in two based on the caret position

I've got a RichTextBox, here referred to as box .

string currentline = box.Lines[box.GetLineFromCharIndex(box.SelectionStart)];

That line there fetches the line the caret is in. It works excellently.

However, I have a need to get two strings from this. The first is everything on that line UP to the caret, and the second is everything on that line AFTER it.

For instance, if the line is How is you|r day going? , with | representing the caret, I would get How is you and r day going? , separately.

I wrote this monstrosity, which works:

string allbefore = box.Text.Substring(0, box.SelectionStart);
string allafter = box.Text.Substring(box.SelectionStart, box.Text.Length - box.SelectionStart);
string linebefore = "";
for (int i = 0; i < allbefore.Length; i++)
{
    linebefore += allbefore[i];
    if (allbefore[i] == '\n')
        linebefore = "";
}
string lineafter = "";
for (int i = 0; i < allafter.Length; i++)
{
    if (allafter[i] == '\n')
        break;
    else
        lineafter += allafter[i];
}

It gives me the result I want, but involves looping through EVERY character in the entire box, which just hurts. Is there an easy way to do this I'm just missing? Thanks.

Have you tried using line.split? Not sure if this is what you want.

This might do the trick for you

string currentline = box.Lines[box.GetLineFromCharIndex(box.SelectionStart)];
var listOfStrings = new List<string>();
string[] splitedBox = currentline.Split('|');
foreach(string sp in splitedBox)
{
    string[] lineleft = sp.Split('\n');
    listOfStrings.Add(lineleft[lineleft.Count() - 1]);
}

In the first approach we are splitting the line by char | than finding if we have any \\n if it exsist we are taking the values accordingly

Another approach could be

string box = "How is \n you|r day \n going?";
bool alllinesremoved = true;
while(alllinesremoved)
{
    if(box.Contains('\n'))
    {
        if(box.IndexOf('\n') > box.IndexOf('|'))
        {
            box = box.Remove(box.IndexOf('\n'), (box.Length - box.IndexOf('\n')));
        }
        else
        {
            box = box.Remove(0, box.IndexOf('\n') + 1);
        }
    }
    else
    {
        alllinesremoved = false;
    }
}
string[] splitedBox = box.Split('|');

in the second approach we are removing the characters before and after the \\n and then splitting the string. I think the second one seems more good to me.

Store the position of \\n using indexOf and, if >= 0, that is, the string contains it, use substring and assign the value otherwise.

string allbefore = box.Text.Substring(0, box.SelectionStart);
string allafter = box.Text.Substring(box.SelectionStart, box.Text.Length - box.SelectionStart);
int newLinePos = allBefore.lastIndexOf("\n");
string lineBefore = ((newLinePos >= 0) ? (allBefore.substring(newLinePos + 1)) : (allBefore));
    newLinePos = allafter.indexOf("\n");
string lineAfter = ((newLinePost >= 0) ? (allAfter.substring(0, newLinePos)) : (allAfter));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM