Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
.NET Framework GroupsView
.NET Distributed_Apps
.NET
.NET ADO.NET
.NET ASP.NET
.NET ASP.NET Security
.NET ASP.NET Webcontrols
.NET ASP.NET Web Services
.NET Clr
.NET Compact Framework
.NET Drawing
.NET Interop
.NET Performance
.NET Web Services
.NET Windows Forms
.NET Windows Forms Controls
.NET General
.NET Csharp
.NET Visual Basic
.NET Vc
.NET Security
.NET Xml
Vsnet Debugging
Xml
Xsl
Scripting Jscript
Scripting Visual Basicscript
Scripting Wsh
Smartphone Developer
Visual Basic Com
Visual Basic Controls
Visual Basic Crystal
Visual Basic Database Ado
Visual Basic Syntax
Visual Basic Winapi
Vc Atl
Vc Debugger
Vc Language
Vc Mfc
Vc Stl
Visio Developer Visual Basica
Windowsce Embedded Vc
Windows Powershell
Visual Basic Vista Compatibility
Deployment Server
.NET Micro Porting

Group SummariesView
.NET Framework
Access
BizTalk
Certifications
CRM
DDK
Exchange Server
FoxPro
French
French .NET
Games
German
German .NET
Graphic Design
IIS
Internet
ISA Server
Italian
Italian .NET
Maps
MCIS
Miscellaneous
Mobile Apps
Money
MSN
Networking
Office
Ops Mgr
Publisher
Security
SharePoint
Small Business
Spanish
Spanish .NET
SQL Server
Systems Management Server
Transaction Server
Virtual PC / Virtual Server
Visual Studio
Win32
Windows 2000
Windows 2003 Server
Windows 7
Windows Live
Windows Media
Windows Update
Windows Vista
Windows XP
 

View All Microsoft NET Csharp Posts  Ask A New Question 

String. Get words ...

Peter Duniho posted on Wednesday, October 08, 2008 2:44 PM

First, you need to decide what you mean by "word".  Then, you need to scan
through the string looking for words.  As you find a complete word, you
then need to append the word to a new string, unless doing so would cause
that new string to exceed your limit of 120 characters, in which case
you're done.

The original string can be a String.  Scanning can be done explicitly
yourself one character at a time, or if you don't mind scanning the entire
input all at once before actually processing the words, you can use the
String.Split() method.  For the purpose of creating the new string as you
go along, I recommend the StringBuilder class.

Note that if you want to preserve the delimiting characters between words
in your output, you'll need to do the scan explicitly rather than using
String.Split(), so that you have access to those characters and can also
append them to your output.

We could just write the code for you but, frankly, we're not on your
payroll and it seems like you ought to go ahead and do _some_ of the work
yourself.  :)

Pete
reply

 

String. Get words ...

shapper posted on Thursday, October 09, 2008 7:55 AM

Hello,

I have a string and I need to get as many words possible counting from
the beginning but without exceeding 120  characters. I can't break
words and I the string shouldn't end with a comma, a dot, ...

How can I do this?

Thank You,
Miguel
reply

String. Get words ...

Duggi posted on Thursday, October 09, 2008 7:55 AM

Now I remember my school days.. My teacher was teaching me
permitations
nPn =3D some formula...

I will try to answer your question soon ...

-Cnu
reply

String. Get words ...

Duggi posted on Thursday, October 09, 2008 7:55 AM

I apologize, this is not to make fun of you,,, Its really a
challenging logic...

-Cnu
reply

String. Get words ...

Peter Duniho posted on Thursday, October 09, 2008 11:04 PM

Sure, that works fine as long as you don't care about character count.
But you do:


So, you need to do what String.Join() does, just write it out yourself and
track the character count too.


You can modify the technique as necessary to do that.  Handling a special
case at the end of the string is easy, and since your punctuation marks
aren't part of the Split() delimiter list, they will remain as part of the
individually split strings.

Pete
reply

String. Get words ...

shapper posted on Sunday, October 12, 2008 12:00 AM

On Oct 8, 7:44=A0pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
can =A0
=A0
=A0
=A0
e =A0
=A0
ou =A0
=A0
=A0
=A0
=A0

I am using the following:

string excerpt =3D String.Join (" ", body.Split (' '), 0, N);

To get the first N words ...

But I am not sure how to count the characters so that excerpt is less
than 120 characters length.

I also need to keep all punctuation marks, I think I am doing that,
with the exception of any that appears at the end ...
...this is because I want to add at the end an ellipsis (...)
reply

String. Get words ...

shapper posted on Sunday, October 12, 2008 12:00 AM

m
scan =A0
ou =A0
se =A0
=A0
=A0
ire =A0
=A0
you =A0
ds =A0
=A0
o =A0
=A0
rk =A0

Or maybe:

private static string Excerpt(string text, int length)
{
char[] chars =3D {'  ', ',', '.', '?', '!', ':', ';'};
int index =3D text.LastIndexOfAny(chars, length);
return text.Substring(0, index);
}

I try to find the last index of a space or of the most comon
punctuation mark in the desired length, for example 120.
Then substring gets the string from i =3D 0 to i =3D index. I suppose this
also removes the last punctuation mark if it exists.
reply