Word substring

text

Sed ut perspiciatis unde omnis iste natus error sit voluptatem ac

I want to fine-tune words differently than word.Substring (1, 29).

regular way:

"Sed ut perspiciatis unde om"

but I want:

"Sed ut perspiciatis unde"

therefore, only complete words are shown. if the word will be cut inside one word before it is shown. I hope I understand what I'm looking for.

+6
c #
source share
2 answers
public static String ParseButDontClip(String original, int maxLength) { String response = original; if (original.Length > maxLength) { int lastSpace = original.LastIndexOf(' ', original.Length - 1, maxLength); if (lastSpace > -1) response = original.Substring(0, lastSpace); } return response; } 

String.LastIndexOf the second parameter is actually the END substring to search for - and the longest - how far back to the beginning you need to go.

Gets me every time I use it.

+12
source share

You can break it into white spaces and then play with an array

+1
source share

All Articles