473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Built in function for text cut-off?

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?
Nov 16 '05 #1
7 2127

"Davids" <db****@simnet.is> wrote in message
news:co**********@news.simnet.is...
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.
Nov 16 '05 #2
Davids wrote:
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
Nov 16 '05 #3
can't see how regex could make a match only after the 300th word?
"Joachim Pense" <sp************@pense-online.de> wrote in message
news:co*************@news.t-online.com...
Davids wrote:
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

Nov 16 '05 #4
Davids wrote:
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
Nov 16 '05 #5
> In Perl, the regex matching the first 300 words would be ^(\W*\w+){300}

Yes, that's what I would have suggested too!

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #6
Davids wrote:
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

Nov 16 '05 #7
Joachim Pense wrote:
Use the regex ^((?:\W+\w+){100}).*


of course it should be 300, not 100.

Joachim
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: toby casey | last post by:
Dear all, I am brand new to php, but learning quickly. I need to be able to go to website, obtain the form elements, cycle through the form elements, fetch the links for each page that results...
1
by: Rigga | last post by:
Hi, I am new to Python and need to parse a text file and cut parts out i.e. say the text file contained 5 rows of text: line 1 of the text file line 2 of the text file line 3 of the text...
1
by: Dennis M. Marks | last post by:
I have a page that uses divisions, tables, iframes, and forms, and is partially built using javascript. As it loads parts of the page appear in odd places and move as other parts are built. How can...
7
by: William Gill | last post by:
Is there a simple way to cut and paste from a tkinter text widget to an entry widget? I know I could create a mouse button event that triggers a popup (message widget) prompting for cut/paste in...
3
by: babylon | last post by:
any facilities in csharp that can help me implmenting undo/redo in my application? thx
8
by: Dennis C. Drumm | last post by:
Is there a way to modify the standard context menu shown when someone right clicks in a windows text box and that would work for all open windows applications? The standard context menu for...
11
by: Graham Charles | last post by:
I'm writing a control inheriting from ComboBox, and I'd like to add an entry to the ContextMenu for that combo box. I have no problem if I create an entirely new ContextMenu from scratch, but I'd...
10
by: ljlolel | last post by:
So.. I have a form that submits to an ASP.net site made in C-sharp. The ASP site is not mine, i do not have the server side code. When I submit from my form by pressing the Submit button, I get...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
4
by: BartlebyScrivener | last post by:
Using Python on Debian Etch. What is the best way to paste a block of text in at the command prompt. I'm trying something like: Quote = raw_input("Paste quote here: ") Which works great...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.