|
-
March 16th, 2012, 11:23 AM
#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
-
March 16th, 2012, 12:24 PM
#2
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/
-
March 17th, 2012, 10:28 PM
#3
Re: String manipulation in c#
 Originally Posted by nileshvk
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
-
March 19th, 2012, 12:21 AM
#4
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.......
-
March 19th, 2012, 09:32 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|