473,320 Members | 1,876 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,320 software developers and data experts.

Finding the character location of the 30th occurance of a substring in a string

The problem I am left with is that I need to split a string into substrings and determine the character location of the 30th occurance of "\n" string.

I have a string that has "\n" within it and I am looking for a way to know how many characters the string has from 0 to the 30th occurance of "\n" within the main string

I have considered a string.split or indexOf but can't wrap my head around how to determine the character location of the 30th occurance part.

Any ideas?
Feb 7 '08 #1
2 1462
Plater
7,872 Expert 4TB
If you're not sure how to use the split(), conisder:
IndexOf() has an overload that takes an index of where to start looking.

So if you have the string:
string s="fred ran fast and fast is good"

and wanted the location of the 4th space " ", you could so something like this:
Expand|Select|Wrap|Line Numbers
  1. int spot=0;
  2. spot=s.IndexOf(" ",spot);
  3. if (spot!=-1)
  4. {//found one, and now spot =4
  5. }
  6.  

So you could loop that with say:
Expand|Select|Wrap|Line Numbers
  1. int spot=-1;
  2. string s="fred ran fast and fast is good";
  3. for(int i=0;i<30;i++)
  4. {
  5.    spot=s.IndexOf(" ",spot+1);//make sure you start on the next character
  6.    if(spot==-1)
  7.    {//could not find another " ", so exit and leave spot at -1
  8.       break;//if you don't exit, it will start over again and possibly give you the wrong value
  9.    }
  10. }
  11. //when the loop exists, spot will either be a -1 meaning there were not enough occurances, or the index where the 30th character is
  12.  

Another possibility is to examine the characters in the string:
Expand|Select|Wrap|Line Numbers
  1. string s="fred ran fast and fast is good";
  2. int count=0;
  3. int spot=-1;
  4. for(int i=0;i <s.Length;i++)
  5. {
  6.    if(s[i]==' ')
  7.    {
  8.       count++;
  9.    }
  10.    if(count==30)
  11.    {
  12.       spot=i;
  13.       break;//leave the for loop
  14.    }
  15. }
  16. //now spot will either be -1 or the index of the 30th " "
  17. //you can adjust to be the \n character you wanted
  18.  

As for Split:
Expand|Select|Wrap|Line Numbers
  1. string s="fred ran fast and fast is good";
  2. int spot=0;
  3. string[] pieces=s.Split(' ');
  4. if (pieces.Length>30)
  5. {//check to make sure there are enough pieces
  6.    for(int i=0;i<pieces.Length;i++)
  7.    {
  8.       //you should add a 1 because the Split() will remove the split character
  9.       spot+=pieces[i].Length+1;
  10.    }
  11. }
  12. else
  13. {
  14.    spot=-1;//not enough
  15. }
  16. //now spot will either be -1 or index of the 30th  " "
  17.  
Feb 7 '08 #2
Yes, this worked.

Thank you.
Feb 13 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Victor Engmark | last post by:
When looking for a method to fetch unique elements and counting the number of occurences of each of them, I found quite a lot of gross examples of complex XSL. But after realizing the subtle...
7
by: news.hku.hk | last post by:
how to count the occurance of a character in a string ? e.g. #include <iostream> #include <string> using namespace std; int main (){ string text = "abc, def, ghi, jkl, mno"; // how to count...
10
by: M Bourgon | last post by:
I'm trying to figure out how to find the last whitespace character in a varchar string. To complicate things, it's not just spaces that I'm looking for, but certain ascii characters (otherwise,...
8
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
4
by: David Warner | last post by:
Greetings! In looking into some C coding, I am looking for the C function that will search for multiple occurances of a same character in a string. For Instance: char str = "We the people...
6
by: Tarun | last post by:
Hi All, I need to find a particular substring in a binary string(some text appended & prepended to binary data). I cant use strstr since it terminates on receiving '\0'which can be there in...
4
by: Michael McCarthy | last post by:
I want to test for the ~possible~ occurance of a string within another string... IndexOfAny gives wildly odd results, possibly because it's expecting the string to be there... This is probably...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
3
by: magix | last post by:
How can I search for occurance of a character in certain position of a string I checked function strchr, but doesn't option to specify position. Thanks. Regards, Magix
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.