473,757 Members | 10,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Position of String Occurence

Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :"

Is there a function to find the position of the "I"th occurence of a
character/string? Like lets say I want to find out the position of the
second colon. How do I go about that?? I can only find functions that
return the position of the first or last occurence, but not a variable
occurence ::sniffles::
Jul 17 '05 #1
2 2466
Tesla wrote:
Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :"

Is there a function to find the position of the "I"th occurence of a
character/string? Like lets say I want to find out the position of the
second colon. How do I go about that?? I can only find functions that
return the position of the first or last occurence, but not a variable
occurence ::sniffles::


Well, you could explode the string at the colons and get

$arr[0] = 'lalala';
$arr[1] = ' djkahsd ';
$arr[2] = ' dajkdassd';
$arr[3] = ' adasd ';
$arr[4] = '';

<?php
$string = "lalala: djkahsd : dajkdassd : adasd :";
$arr = explode(':', $string);
?>

But, if you really want the position of the nth ocurrence of needle in
haystack, I guess your best bet is to make a function that returns that
(or false if not enough needles are in haystack)

or use the array above and get the position as a sum of strlen()s, but I
prefer a specific function :)
Happy Coding :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Hmm, how about exploding the string by colons then calculating the length of
the first element to work out the beginning of the second? I'm no expert but
that's what I came up with...
"Tesla" <tp**@houston.r r.com> wrote in message
news:Tk******** *********@fe2.t exas.rr.com...
Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :"

Is there a function to find the position of the "I"th occurence of a
character/string? Like lets say I want to find out the position of the
second colon. How do I go about that?? I can only find functions that
return the position of the first or last occurence, but not a variable
occurence ::sniffles::

Jul 17 '05 #3

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

Similar topics

1
2593
by: Eshrath Ali Khan | last post by:
Hi, I have a requirement where I am transforming a XML into a html using an XSL. I need to create a object for the each and every occurrence of an element named "tgroup" in the xml. Also I need to assign a unique id for this object based upon the its occurence in the xml document. (note: the unique id should not be assigned depeneding upon its position with respect to the parent element but with respect to its number of occurence in the...
2
1761
by: Ney André de Mello Zunino | last post by:
Hello. To find the position of an element within a collection, I currently follow these two steps: 1. obtain an iterator to the element (i.e. find it) 2. use std::distance with coll.begin() and the previous iterator This works, but I still would like to know if there is a better way which I might be overlooking.
4
1437
by: Yves-Alain NICOLLET | last post by:
I have a script that opens a small window containing a button which when clicked performs a find to the next occurence of a string. It works well when the string is made of only one word or of consecutive words. Now I want it to find the next occurence of one of the words contained in a list, ie. I want to dynamically change the string to find. I failed to use a regular expression instead of a string in the find statement. Any idea of...
4
14128
by: sibingpeter | last post by:
Hi there, Im trying to find the right way to code the loop to count the number of occurences of a given substring in a string. Im able to find the first occurence using the strstr function, but I just cant seem to think of the right loop that would continue searching after finding this first occurence. Could someone please help me out here?
2
4211
by: scottys0 | last post by:
Hi everyboy, I'm working in an IOC-IBM (IBM open class) replamecent library. the code seens confused but , no, isn't. #define INumber long class IString : public std::string { private: ... // some control variables
2
1975
by: DaFerg | last post by:
Hi ... I have a weblog database where I want to count the occurences of a table of string values that appear in all the urls viewed. My tblWebLog as a field that contains the url ... tblWebLog. I have another table ... tblStrings ... that has a field for a string value and an integer field to count the occurence of the string in tblWebLog.
3
4061
by: magix | last post by:
Dear Guru, I have been thinking hard on how to token based on demiliter after certain position. Example, I have list of possible string below, and the the delimiter is "1" with the rules below - if the 5th character is "1", then it is delimiter elseif the 7th character is "1", then it is delimiter else this string is not a valid string
1
1139
by: StepIre | last post by:
Hey Guys, Sorry if this is silly question but i am new to all of this! Basically i have a file that has some content in it that i want to get out. i want to find say an occurence of the string "blah". then i want to find the next occurence of the string "blah2" after that.
3
3568
by: lilly07 | last post by:
I am trying open a text file, and find the occurence of first column string (ie) $v in a separate file which contains only one column. my code is as follows but somehow it doesn't seem to count the number of occurences in the second file. #!/usr/bin/perl -w $datafile = $ARGV; print "$datafile\n"; open (LIST1, $datafile) || die "File not found\n";
0
9489
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.