String manipulation/splitting

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.ToString().IndexOf("\\\\") >= 0)
{
splitLines = lines.ToString().Split("\\\\".ToCharArray());
for(int j = 0; j < splitLines.Length; j++)
{
linesList.Add(splitLines[j]);
}
}
else
{
linesList.Add(lines);
}
}
StringBuilder builder = new StringBuilder();
for(int i = 0; i < linesList.Count; i++)
{
builder.Append(linesList.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
 
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
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.ToString().IndexOf("\\\\") >= 0)
{
splitLines = lines.ToString().Split("\\\\".ToCharArray());
for(int j = 0; j < splitLines.Length; j++)
{
linesList.Add(splitLines[j]);
}
}
else
{
linesList.Add(lines);
}
}
StringBuilder builder = new StringBuilder();
for(int i = 0; i < linesList.Count; i++)
{
builder.Append(linesList.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
 
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



Morten Wennevik said:
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
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.ToString().IndexOf("\\\\") >= 0)
{
splitLines = lines.ToString().Split("\\\\".ToCharArray());
for(int j = 0; j < splitLines.Length; j++)
{
linesList.Add(splitLines[j]);
}
}
else
{
linesList.Add(lines);
}
}
StringBuilder builder = new StringBuilder();
for(int i = 0; i < linesList.Count; i++)
{
builder.Append(linesList.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

 
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.

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



Morten Wennevik said:
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
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.ToString().IndexOf("\\\\") >= 0)
{
splitLines = lines.ToString().Split("\\\\".ToCharArray());
for(int j = 0; j < splitLines.Length; j++)
{
linesList.Add(splitLines[j]);
}
}
else
{
linesList.Add(lines);
}
}
StringBuilder builder = new StringBuilder();
for(int i = 0; i < linesList.Count; i++)
{
builder.Append(linesList.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

 
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

Morten Wennevik said:
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.

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



Morten Wennevik said:
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
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.ToString().IndexOf("\\\\") >= 0)
{
splitLines = lines.ToString().Split("\\\\".ToCharArray());
for(int j = 0; j < splitLines.Length; j++)
{
linesList.Add(splitLines[j]);
}
}
else
{
linesList.Add(lines);
}
}
StringBuilder builder = new StringBuilder();
for(int i = 0; i < linesList.Count; i++)
{
builder.Append(linesList.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

 
Back
Top