473,320 Members | 2,088 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.

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 2426
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.rr.com> wrote in message
news:Tk*****************@fe2.texas.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
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...
2
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()...
4
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...
4
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...
2
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:...
2
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....
3
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...
1
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...
3
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...
0
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...
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: 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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.