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.
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".
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."
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With