Built in function for text cut-off?

  • Thread starter Thread starter Davids
  • Start date Start date
D

Davids

does C# (or even .Net) have some built in function to make a cut off in a
lengthy text, eg cut all text behind the last space (or carriage return)
occuring after - say - the 300th word? I know how to code this, just thought
there'd be some built in function?
 
Davids said:
does C# (or even .Net) have some built in function to make a cut off in a
lengthy text, eg cut all text behind the last space (or carriage return)
occuring after - say - the 300th word? I know how to code this, just
thought there'd be some built in function?

No, I do not believe there is any intrinsic.
 
Davids said:
does C# (or even .Net) have some built in function to make a cut off in a
lengthy text, eg cut all text behind the last space (or carriage return)
occuring after - say - the 300th word? I know how to code this, just
thought there'd be some built in function?

Could the Regex.Replace(string input, string pattern, string replacement)
function be used?

I do not know the power of the regex engine in C# though (it certainly would
be possible to do in a single regex in Perl).

Joachim
 
Davids said:
can't see how regex could make a match only after the 300th word?

In Perl, the regex matching the first 300 words would be ^(\W*\w+){300}, but
I did not test this. There are ways to get the part *after* this match, but
they are probably peculiar to Perl. I'll have a look how to do it in C# (to
which I am new, so this will be a good exercise for me).

Joachim
 
In Perl, the regex matching the first 300 words would be ^(\W*\w+){300}

Yes, that's what I would have suggested too!
 
Davids said:
can't see how regex could make a match only after the 300th word?

I found it. (What I learned from all this was that more or less the full
power and Perl's regular expressions has been put into C# without changing
their syntax, and that the way to use them in C# is of course less elegant
than in Perl, but quite logical once you got through all those classes
involved.

Use the regex ^((?:\W+\w+){100}).*
with the replacement pattern $1

The (?: ... ) defines a non-capturing group, multiplied by {100} to capture
100 words.

It is enclosed in a capturing group ( ... ) that defines $1 as the first
captured part (which consists of the first 100 words).

The complete program (with 5 instead of 100 words):

//////////////////////////////////////////////////////////////
using System;
using System.Text.RegularExpressions;

class MainClass
{
public static void Main(string[] args)
{
string texample =
@" abc,; def... : ghi jkl ! mno pqr stu vwx yz";
Regex rexample = new Regex(@"^((?:\W+\w+){5}).*");
string replaced = rexample.Replace(texample, "$1");
Console.WriteLine(texample);
Console.WriteLine(replaced);
}
}
//////////////////////////////////////////////////////////////

The output:
abc,; def... : ghi jkl ! mno pqr stu vwx yz
abc,; def... : ghi jkl ! mno

Joachim
 
Back
Top