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.

Check This code

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

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

Check This code

Your code won't compile :)

It will Momerath i just checked it before pasting :) the typing mistake is removed now :D

Solved Thanks to abelLazm. :D

You helped so much!

-Cheers

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

How do I do that? Haha...

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 is the image of what I say

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.