473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

split string by indexOf

N9
Hi

Anyone who can help about split string.

string text = "History about a boy, who loves to play baseball with
his friends."

I like to find indexOf "play" and read the string 10 char left and 10
char right

The result: " loves to play baseball "

but my problem is, if I indexOf "his", it fail, because, " friends."
only had 9 char.

It's the same problem at the start of text.

anyone who can help ??

//N9
Jun 27 '08 #1
4 7709
"N9" <nh****@gmail.comwrote:
but my problem is, if I indexOf "his", it fail, because, " friends."
only had 9 char.
Count through the string with a loop and stop if you go out of range, or
add 10 spaces at start and end and trim the result.

Eq.
Jun 27 '08 #2
Smells like homework?

You need to adjust the ends if they go outside of the range...

int pre = 10, post = 10;
string text = "History about a boy, who loves to play baseball
with his friends.",
search = "play";
int index = text.IndexOf(search);
int start = index - pre, end = index + search.Length + post;
if (start < 0) start = 0;
if (end text.Length) end = text.Length;
string subString = text.Substring(start, end - start);

Marc
Jun 27 '08 #3
On May 14, 9:44 am, Marc Gravell <marc.grav...@gmail.comwrote:
Smells like homework?

You need to adjust the ends if they go outside of the range...

int pre = 10, post = 10;
string text = "History about a boy, who loves to play baseball
with his friends.",
search = "play";
int index = text.IndexOf(search);
int start = index - pre, end = index + search.Length + post;
if (start < 0) start = 0;
if (end text.Length) end = text.Length;
string subString = text.Substring(start, end - start);
An alternative to the last four lines:

int start = Math.Max(0, index-pre);
int end = Max.Min(text.Length, index+pre);
string subString = text.Substring(start, end-start);

Jon
Jun 27 '08 #4
On May 14, 11:47 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
int end = Max.Min(text.Length, index+pre);
Sorry, this should be index+post, not index+pre.

Jon
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
1
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As...
1
by: mad.scientist.jr | last post by:
I am working in C# ASP.NET framework 1.1 and for some reason Regex.Split isn't working as expected. When trying to split a string, Split is returning an array with the entire string in element ...
1
by: mad.scientist.jr | last post by:
I am working in C# ASP.NET framework 1.1 and for some reason Regex.Split isn't working as expected. When trying to split a string, Split is returning an array with the entire string in element ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.