Author Topic: [C#]Extract Information From String Using '[' & ']'  (Read 651 times)

0 Members and 1 Guest are viewing this topic.

Offline Huntondoom

  • 0x13338
  • ******
  • Posts: 828
  • Karma: +18/-0
  • Gender: Male
  • Visual C# programmer
    • View Profile
[C#]Extract Information From String Using '[' & ']'
« on: November 05, 2011, 05:42:18 PM »
for a recent project I came up with this function
look beneath it for information on how it work

Code: [Select]
private string ExtractInformation(string Post)
        {
            List<string> Objects = new List<string>();
            List<string> EndResult = new List<string>();
            for (int A = 0; A < Post.Length - 1; ++A)
            {
                if (Post.Substring(A, 1) == "[" & Post.Substring(A,2)!="[/")
                {
                    for (int B = A; B < Post.Length - 1; ++B)
                    {
                        if (Post.Substring(B, 1) == "]")
                        {
                            string Item = Post.Substring(A + 1, B - (A + 1));
                            if (!Objects.Contains(Item) & Item.Length > 1) { Objects.Add(Item); }                             
                            break;
                        }
                    }
                }
            }
            for (int A = 0; A < Objects.Count; ++A)
            {
                string StartString = "[" + Objects[A] + "]";
                string EndString = "[/" + Objects[A] + "]";
                string AltEnd = "[/]";

                for (int B = 0; B < Post.Length - StartString.Length; ++B)
                {
                    if (Post.Substring(B, StartString.Length) == StartString)
                    {
                        int C = B;
                        while (C < Post.Length)
                        {
                            if (C < Post.Length - EndString.Length)
                            {
                                if (Post.Substring(C, EndString.Length) == EndString)
                                {
                                    EndResult.Add(Objects[A] + "=" +Post.Substring(B + StartString.Length , C - (B + StartString.Length)));
                                    break;
                                }
                            }
                            if (C < Post.Length - AltEnd.Length)
                            {
                                if (Post.Substring(C, AltEnd.Length) == AltEnd)
                                {
                                    EndResult.Add(Objects[A] + "=" + Post.Substring(B + StartString.Length, C - (B + StartString.Length)));
                                    break;
                                }
                            }
                            ++C;
                           
                        }
                    }
                }
            }
            StringBuilder NewPost = new StringBuilder();
            for (int A = 0; A < EndResult.Count; ++A)
            {
                NewPost.AppendLine(EndResult[A]);
            }
            return NewPost.ToString();
        }

this functions retrieve all of the information that is 'labeled'.
lets say we have a piece of text that is label using words that are located between
'[' & ']'
Quote
[Username]Huntondoom[/Username] [Derp]Hello everyone this is some shit[/] how do you do? [Lolz] this works! [/Lolz]

this function will then look for all the Headers that are used (as in getting the words between '[' & ']')
but if the text in between is 1 character big it will be skipped and found unworthy
all of these 'Headers' will be stored in a list so it would look like this
Quote
Username
Derp
Lolz

then It will retrieve the information that is located between a headers and its counterpart (usually its the header with a '/' infront of it)
but it also response against a universal counterpart = '[/]'
so you dont need to put in stuff like '[/Username]' but just '[/]'
this all will be stored inside of a list, along side its header that said it should be Extracted.
so:
Quote
Username=Huntondoom
Derp=Hello everyone this is some shit
Lolz=this works!

and that will be converted to a string and returned
« Last Edit: November 05, 2011, 05:42:59 PM by Huntondoom »
Aslong as you are connected to the internet, you'll have no privacy

Advanced Internet Search
Clean Up!

Offline bubzuru

  • Int
  • **
  • Posts: 147
  • Karma: +6/-0
  • everything is contained in the data
    • View Profile
    • New School Tools
Re: [C#]Extract Information From String Using '[' & ']'
« Reply #1 on: November 05, 2011, 06:02:53 PM »
nice function
but wouldnt it be much better to use .Split() ??
Damm it feels good to be gangsta
http://bubzuru.comule.com

Offline xzid

  • VIP
  • Long[]
  • *
  • Posts: 304
  • Karma: +37/-4
    • View Profile
Re: [C#]Extract Information From String Using '[' & ']'
« Reply #2 on: November 05, 2011, 08:53:02 PM »
If you were to substitute '[' for '<' and get rid of [/], you could parse it using System.Xml

Example in powershell(PS has xml data type, makes things quick):

Code: [Select]
PS K:\>$txt = $txt -replace "\[", "<"
PS K:\>$txt = $txt -replace "\]", ">"
PS K:\>$txt
<Username>Huntondoom</Username> <Derp>Hello everyone this is some shit</Derp> how do you do? <Lolz> this works! </Lolz>
PS K:\>$xml = [xml]"<myxml>$txt</myxml>"
PS K:\>$xml.myxml.Username
Huntondoom
PS K:\>$xml.myxml.Derp
Hello everyone this is some shit
PS K:\>$xml.myxml.Lolz
 this works!
PS K:\>$xml.myxml."#text"
 how do you do?

markup sucks ass to parse, and becomes alot harder(+ more buggy code) when you start using string functions instead of a proper library. regex could also work, but personally I only use it in scripting.

Offline Huntondoom

  • 0x13338
  • ******
  • Posts: 828
  • Karma: +18/-0
  • Gender: Male
  • Visual C# programmer
    • View Profile
Re: [C#]Extract Information From String Using '[' & ']'
« Reply #3 on: November 06, 2011, 01:12:00 PM »
If you were to substitute '[' for '<' and get rid of [/], you could parse it using System.Xml
I know but, its a pain in the ass since I have a function before this that gets rid of everything that between '<' & '>', I have used Replace(,) to mark certain stuff to be important and the rest can be deleted :P
Aslong as you are connected to the internet, you'll have no privacy

Advanced Internet Search
Clean Up!

Offline Unbr0ken

  • NOP
  • Posts: 14
  • Karma: +1/-0
  • Gender: Male
    • View Profile
Re: [C#]Extract Information From String Using '[' & ']'
« Reply #4 on: December 20, 2011, 08:50:18 PM »
Isn't so much better using the Regular Expressions?
« Last Edit: December 20, 2011, 08:51:05 PM by Unbr0ken »
[C] -> [C++] -> [C#]

Forgive
     To

 Forget

Offline ande

  • Administrator
  • 0x13338
  • *
  • Posts: 1404
  • Karma: +80/-7
  • Gender: Male
    • View Profile
    • Evilzone
Re: [C#]Extract Information From String Using '[' & ']'
« Reply #5 on: December 20, 2011, 09:08:55 PM »
Probably is. /\[(.*)+\](.*)?\[$1\]/ims (not sure if you can use $1 tho, idea was that both the [aaa] is alike)
« Last Edit: December 20, 2011, 09:10:05 PM by ande »

 



Intern0t SoldierX py1337 SecurityOverride programisiai
Want to be here? Contact Ande or Satan911 on the forum or at IRC.