Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

substring for words

Tags:

c#

text:

Sed ut perspiciatis unde omnis iste natus error sit voluptatem ac

i want to substring words not in regular way like word.Substring(1, 29).

regular way:

"Sed ut perspiciatis unde om"

but i want:

"Sed ut perspiciatis unde"

so only full words are shown. if word is cut inside one word before will be shown. hope understand what i am looking for.

like image 773
senzacionale Avatar asked Mar 03 '11 20:03

senzacionale


People also ask

What is a substring of a word?

In formal language theory and computer science, a substring is a contiguous sequence of characters within a string. For instance, "the best of" is a substring of "It was the best of times".

What is an example of a substring?

A substring is a subset or part of another string, or it is a contiguous sequence of characters within a string. For example, "Substring" is a substring of "Substring in Java."

How do you substring a word in Java?

The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1 if the second argument is given. Syntax : public String substring(int begIndex, int endIndex) Parameters : beginIndex : the begin index, inclusive. endIndex : the end index, exclusive.

How do you find the substring of a string?

You first check to see if a string contains a substring, and then you can use find() to find the position of the substring. That way, you know for sure that the substring is present. So, use find() to find the index position of a substring inside a string and not to look if the substring is present in the string.


1 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 second's parameter is actually the END of the substring to search for - and longest is how far back towards the start you need to go.

Gets me every time I use it.

like image 65
John Arlen Avatar answered Nov 15 '22 07:11

John Arlen