Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2005

Tip: Looking for answers? Try searching our database.

String manipulation/splitting

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Raj Kumar - 05 Jul 2005 21:35 GMT
Hi!
I need to parse some text in a rich text box and store it in an array, each
item of which will be stored in a database table as a separate record.
For. eg If the user enters this text:
We, \\parents, invite you
for the wedding of our son, \\groom
on \\date.

Then I need to parse this text so that it is stored in the array as:
We,
\\parents,
invite you
for the wedding of our son,
\\groom
on
\\date.

Using the split method I am able to split the text but I lose the slashes.
This is the code I have working now:
string[] lines = new string[50];
lines = textToParse.Split("\r\n".ToCharArray());
ArrayList linesList = new ArrayList();
string [] splitLines = new string[50];

for(int i = 0; i < lines.Length; i++)
{
    if(lines[i].ToString().IndexOf("\\\\") >= 0)
    {
        splitLines = lines[i].ToString().Split("\\\\".ToCharArray());
        for(int j = 0; j < splitLines.Length; j++)
        {
            linesList.Add(splitLines[j]);
        }
    }
    else
    {
        linesList.Add(lines[i]);
    }
}
StringBuilder builder = new StringBuilder();
for(int i = 0; i < linesList.Count; i++)
{
    builder.Append(linesList[i].ToString());
    builder.Replace("\n","",builder.Length - 1, 1);
    builder.Append("\n");
}
builder.Remove(builder.Length - 1, 1);

I realize this is not the best way to do it, so I need some help/suggestions
on how to handle this scenario.

Thanks for your time.

Regards,
Kumar
Morten Wennevik - 05 Jul 2005 23:10 GMT
Hi Kumar,

I wonder if you're not simply looking for String.Replace/Regex.Replace, and swap the names\addresses with a list in a loop.

> Hi!
> I need to parse some text in a rich text box and store it in an array, each
[quoted text clipped - 51 lines]
> Regards,
> Kumar

Signature

Happy coding!
Morten Wennevik [C# MVP]

Raj Kumar - 08 Jul 2005 17:55 GMT
I am not sure if I understand what you are asking me try. Nevertheless, I was
able to simplify the process by using Regex.

Regex exp = new Regex(@"\\\\|\n");
string [] finalText = exp.Split(textToParse);
Now with this in place, when I feed a text like:
We, \\parents, invite you
for the wedding of our son,
\\groom on \\date.

I get this:

We,
parents, invite you
for the wedding of our son,
groom on
date.

and what I want is:

We,
parents, invite you
for the wedding of our son,
groom
on <----------------------separate line
date.

Does anyone have any idea?

Thanks and regards,
Kumar

> Hi Kumar,
>
[quoted text clipped - 55 lines]
> > Regards,
> > Kumar
Morten Wennevik - 08 Jul 2005 21:20 GMT
Ok, I think I understand you now.

You want to take a regular text including linebreaks and if there is a keyword marked with \\ insert a linebreak before and after, unless one is already present.

I'm not aware of any simple way to do this, which doesn't mean there isn't one (I'm not too familiar with regex expressions).  However, there shouldn't be much of a problem to do it manually.  Loop through the file, if \\ is encountered, find the location of the next space or linebreak (which is \r\n in Windows, not just \n).  If space is found, insert linebreak, if linebreak is found, continue.

> I am not sure if I understand what you are asking me try. Nevertheless, I was
> able to simplify the process by using Regex.
[quoted text clipped - 91 lines]
>> Happy coding!
>> Morten Wennevik [C# MVP]

Signature

Happy coding!
Morten Wennevik [C# MVP]

Raj Kumar - 13 Jul 2005 20:03 GMT
Thank you for your suggestions. With the help of another developer's
suggestions, I used Match method of Regex object and also used Replace method
of the string object and got it working.

Regards,
Kumar

> Ok, I think I understand you now.
>
[quoted text clipped - 97 lines]
> >> Happy coding!
> >> Morten Wennevik [C# MVP]

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.