views:

65

answers:

5

I'm getting a string similar to the following back from the database:

The object of the following is to do:
* blah 1 * blah 2 * blah 3 * blah 4. Some more extremely uninteresting
text. Followed by yet another sentence
full of extremely uninteresting text.
Thankfully this is the last sentence.

I need to format this so that each * represents a bullet point, and the sentence after the last * goes onto a new line, ideally as follows:

The object of the following is to do: 

* blah 1
* blah 2 
* blah 3 
* blah 4. 

Some more extremely uninteresting
text. Followed by yet another sentence
full of extremely uninteresting text.
Thankfully this is the last sentence.

It's easy enough to split the string by the * character and replace that with <br /> *. I'm using the following for that:

string description = GetDescription();

description = description.Replace("*", "<br />*"); // it's going onto a web page.

but the result this gives me is:

The object of the following is to do: 

* blah 1 
* blah 2 
* blah 3 
* blah 4. Some more extremely uninteresting text. Followed by yet
another sentence full of extremely
uninteresting text. Thankfully this is
the last sentence.

I'm having a bit of difficulty identifying the fist sentence after the last '*' so I can put a break there too. Can somebody show me how to do this?

+4  A: 

You need some way to determine the other delimiter in your last sentence. You could do a global replace for * to <br /> * as you do currently, then find the last instance of * and replace the next . or whatever delimiter with . <br />.

Update

Fixed the order of <br /> . to . <br /> as @Shaded pointed out.

Andy
You'd probably actually want to replace it with '. <br />' so that the period doesn't jump with the last sentence.Still +1 cause that's what I would do.
Shaded
A: 

You would need to add another * after the blah 4

Wildhorn
If I added another * after the blah 4, it would be replaced by "<br /> *", so there would be a * leading the sentences following the bullets
DaveDev
+2  A: 

If it can be assumed that the fist sentence after the last '*' is separated by a period - then you can do the following:

  1. Replace the input by finding all * characters
  2. Find the last <br> and then find the first . after it.
  3. Recombine the fragments, replacing * with <br/>*

Here's some code to get you going: (it may not be perfect). I'm using string.Split() here rather than string.Replace() because I find it makes the logic easier to follow. The trick is that the last subRegion consists of two parts (the bullet and the sentence after the bullet).

string inputText = "...";
string[] subRegions = inputText.Split( "*" );
// split final region into bullet and sentance based on first period...
string[] lastRegions = subRegions[subRegion.Length-1].Split(new[]{'.'},2);
// replace the final subregion with the combined parts (bullet/sentence)
subRegions[subRegions.Length-1] = string.Join(". <br/>",lastRegions);
// combine all subregions together, replacing `*` with bulletized breaks.
string finalText = string.Join( "<br/>*", subRegions );
LBushkin
A: 

If you can assume that the last bullet ends with a '.', then something like this might work:

string description =
    "The object of the following is to do: * blah 1 * blah 2 * blah 3 * blah 4. Some more extremely uninteresting text. Followed by yet another sentence full of extrememly uninteresting text. Thankfully this is the last sentence.";            
int idx = description.IndexOf('.', description.LastIndexOf('*'));
description = description.Insert(idx+1, "<br />");
description = description.Replace("*", "<br />*");

Basically it finds the first occurrence of '.' after the last '*. If you have more separator characters you could use those as well.

Mikael Svenson
A: 

Not being a C# expert but I'd be thinking to "split" the string on "*" into an array to get all your bullets and then either split the last item in the array (your last bullet with the sentence) or use a regex to grab the sentence.

i.e. Pseudo code

List bullets = description.split('*');
String lastSentence = bullets(bullets.size() + 1).replace('^\*.*?\.', '');

Where the regex is meant to strip *anything up to the first fullstop.

Of course this approach might not work if your laast sentence has a '*' in it of course :)

Lawrence Tierney