Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7565905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:16:37+00:00 2026-05-30T14:16:37+00:00

For example, if there was a TextBox that contained this text: The quick brown

  • 0

For example, if there was a TextBox that contained this text:

The quick brown fox jumped over the lazy dog.

And the blinking cursor (text caret) of the textbox was located at the i in quick, what would be an elegant way of finding the index for the q and the index for the k?

Basically, given a position in the string, how would I find the index of the beginning and end of the word? If the given index is a space assume it uses the previous word.

I have a really hackish solution using loops going in both direction until each finds a space but it’s really messy and I was wondering if there was a more simplistic way to approach the problem. It’s late so I feel like I may be missing something obvious here.

Thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:16:38+00:00Added an answer on May 30, 2026 at 2:16 pm

    Using loops in both directions sounds like the right approach to me – but you don’t need to write the loops. You can use String.IndexOf and String.LastIndexOf:

    int nextSpace = text.IndexOf(' ', index);
    int previousSpace = text.LastIndexOf(' ', index);
    

    It will be fiddly, and you should also consider that words don’t always break at space boundaries – but that should at least get rid of your loops 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to explain this by example: Is there a best practice or preference
For example if in a textbox that is full of many different part numbers
I have some dynamic text contained in a div that is set to whatever
I want to make the textbox allow only three digits and three decimals.For example
I will explain problem with an example: There is two table in my database,
At http://docs.jquery.com/UI/Tabs#Example there is a short HTML example of how to use JQuery UI
On the Groovy example page there is an example of how to use Groovy
Is a C# struct thread-safe? For example if there is a: struct Data {
Is there good example code or a test project for explaining the Model–view–presenter (MVP)
Is there an example of an OAuth implementation or profile which uses multiple authorization

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.