Hey, Im trying to figure out how to read a starting and ending point in a string. What Im trying to have accomplished is something like this (This code doesn't work)

string str = "";

//str = readfrom(textBox1.Text, "start Character", "End Character");
str = readfrom(textBox1.Text, "[", "]");

textbox2.text = str;

So if you entered this into textbox 1

ehaoighaeogiuaehga[SupDaniweb?]euj38921t891ht3t

then Textbox 2 would read

SupDaniweb?

Or whatever you put in the []

This would help me out a lot, Thanks :D

-Cheers

While there is a way to do it, it looks messy, so best* thing is to create an extension method!

namespace extensions;

public static class MyStringExtensions {
    public static String ReadFrom(this string str, char start, char end) {
        string result = null;
        try {
            int s = str.IndexOf(start);
            int e = str.IndexOf(end, start);
            result = str.Substring(start, end-start+1);
        } catch (ArgumentOutOfRangeException e) {
            // one or both of the characters doesn't exist in the string
            // but we are going to ignore this and just return null
        }

        return result;
    }
}

Once this is in it's own file in your project, you'll be able to do this:

string whatever = firstString.ReadFrom('[', ']');

* 'best' is a relative term. This is a way to do it without regex, which would just complicate everyones life.

Edited 5 Years Ago by Momerath: n/a

Check This code

int index = textBox3.Text.IndexOf('[');
                int length1 = (textBox3.Text.IndexOf(']')-index)+1;

                MessageBox.Show(textBox3.Text.Substring(index,length1));

Edited 5 Years Ago by abelLazm: n/a

Your are always welcome :) If solved then please mark it solved

Edited 5 Years Ago by abelLazm: n/a

above the message box where you right your post there are few lines written with the heading is this thread answered in very first line there is a link Mark this thread as solved click it

This question has already been answered. Start a new discussion instead.