CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2012
    Posts
    1

    String manipulation in c#

    Hi All,

    I need assistance in manipulating below string.

    <FONT size=4>This is my line 1<BR><BR><FONT style="BACKGROUND-COLOR: #ffff80">This is my line 2</FONT></FONT><BR>

    Output should be:

    string l1=This is my line 1;

    string l2 = This is my line 2;

    Pl' note length of each string may vary.

    Thanks,

    Nilesh

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: String manipulation in c#

    And what have you tried so far? What are you trying to do with this string?
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  3. #3
    Join Date
    Mar 2012
    Posts
    17

    Re: String manipulation in c#

    Quote Originally Posted by nileshvk View Post
    Hi All,

    I need assistance in manipulating below string.

    <FONT size=4>This is my line 1<BR><BR><FONT style="BACKGROUND-COLOR: #ffff80">This is my line 2</FONT></FONT><BR>

    Output should be:

    string l1=This is my line 1;

    string l2 = This is my line 2;

    Pl' note length of each string may vary.

    Thanks,

    Nilesh
    well this how i do it..

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace nileshvk
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                String data = "<FONT size=4>This is my line 1<BR><BR><FONT style=\"BACKGROUND-COLOR: #ffff80\">This is my line 2</FONT></FONT><BR>";
                String result = "";
                data = data.Replace("><", "");
                /*data = data.Substring(data.IndexOf(">") + 1);
                data = data.Substring(0,data.LastIndexOf("<"));*/
    
                int temp=1;
    
                foreach (char x in data) 
                {
                    if (temp == 0)
                    {
                        if (x != '<')
                        {
                            result += x;
                        }
                        else
                        {
                            result+='#';
                            temp = 1;
                        }
                    }
                    else if(x == '>')
                    {
                        temp = 0;
                    }
                }
    
                String[] line = result.Split('#');
    
                foreach(string x in line)
                    Console.WriteLine(x);
    
                Console.ReadLine();
            }
        }
    }
    its simple as its tag from web if im recall it right.
    they always "<b><c>text</c></b>"

    this means if i take out the "><" = i make they became 1 tag only
    it will be like <>text1<>text2<>text3<>....<> always open with tag and end with tag..
    so using my logic i will take the text between > and <
    no matter if they was zero length ..

    after finish all you will find the arrays of string is more than we want. so
    you can use a validation either the length of the string is zero or not before use it..

    hope this help

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: String manipulation in c#

    Not sure how bullet proof this is, but it works for your sample. Probably best if you also include a flag to say whether you are inside or outside a tag. Then you can report an error (if desirable) should you get << or >>.
    Code:
    using System;
    using System.Collections.Generic;
    
    namespace ComfortablyNumb
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<string> result = new List<string>();
    
                string data = "<FONT size=4>This is my line 1<BR><BR><FONT style=\"BACKGROUND-COLOR: #ffff80\">This is my line 2</FONT></FONT><BR>";           
                string currentLine = string.Empty;
    
                foreach (char ch in data)
                {
                    if (ch == '<')
                    {
                        if (currentLine != string.Empty) 
                            result.Add(currentLine);
                    }
                    else if (ch == '>')
                        currentLine = string.Empty;
                    else
                        currentLine += ch;
                }
    
                if (currentLine != string.Empty) 
                    result.Add(currentLine);
    
                foreach (string line in result)
                    Console.WriteLine(line);
    
                Console.Read();
            }
        }
    }
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #5
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: String manipulation in c#

    I'd use string.Split( ) twice. The first time using '<' as a separator, the second using '>'. The final set of elements should have a first item containing the tag attributes, and a second item that could either be empty or contain your lines.

    Using '<'
    Code:
    Item 1:
    FONT size=4>This is my line 1
    Item 2:
    BR>
    Etc.
    Then using '>'
    Code:
    Subitem 1 of item 1:
    FONT size=4
    Subitem 2 of item 1:
    This is my line 1
    
    Subitem 1 of item 2:
    br
    Subitem 2 of item 2:
    <empry>
    Subitem 2 should always be the one you're after, except if it's empty.

    Perhaps Split has other better uses, or perhaps you can find out a way to use "><" as a splitter? It all depends on the more general scenarios your input lines could present.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured