Find Nth Occurrence of a Character in a String

Find the nth occurrence of substring in a string

Mark's iterative approach would be the usual way, I think.

Here's an alternative with string-splitting, which can often be useful for finding-related processes:

def findnth(haystack, needle, n):
parts= haystack.split(needle, n+1)
if len(parts)<=n+1:
return -1
return len(haystack)-len(parts[-1])-len(needle)

And here's a quick (and somewhat dirty, in that you have to choose some chaff that can't match the needle) one-liner:

'foo bar bar bar'.replace('bar', 'XXX', 1).find('bar')

Get the index of the nth occurrence in a string

You can use split, slice & join to achieve your requirement.

Logic

First split your string with char then use slice to join split values upto nth occurrence. Then simply join with char. It's length will be your answer.

Check below.

function getIndex(str, char, n) {  return str.split(char).slice(0, n).join(char).length;}
console.log(getIndex('https://www.example.example2.co.uk', '.', 2)) // returns 19console.log(getIndex('https://www.example.example2.co.uk', '.', 1)) // returns 11console.log(getIndex('https://www.example.example2.co.uk', '.', 3)) // returns 28

How to get the nth occurrence in a string?

const string = "XYZ 123 ABC 456 ABC 789 ABC";
function getPosition(string, subString, index) { return string.split(subString, index).join(subString).length;}
console.log( getPosition(string, 'ABC', 2) // --> 16)

Find Nth occurrence of a character in a string

public int GetNthIndex(string s, char t, int n)
{
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == t)
{
count++;
if (count == n)
{
return i;
}
}
}
return -1;
}

That could be made a lot cleaner, and there are no checks on the input.

Finding the nth occurrence of a character in a string in javascript

function nth_occurrence (string, char, nth) {
var first_index = string.indexOf(char);
var length_up_to_first_index = first_index + 1;

if (nth == 1) {
return first_index;
} else {
var string_after_first_occurrence = string.slice(length_up_to_first_index);
var next_occurrence = nth_occurrence(string_after_first_occurrence, char, nth - 1);

if (next_occurrence === -1) {
return -1;
} else {
return length_up_to_first_index + next_occurrence;
}
}
}

// Returns 16. The index of the third 'c' character.
nth_occurrence('aaaaacabkhjecdddchjke', 'c', 3);
// Returns -1. There is no third 'c' character.
nth_occurrence('aaaaacabkhjecdddhjke', 'c', 3);

Find nth occurrence of a character in a row and replace it in Python, in order to fix/replace a wrong/leftover ; separator in a text file

You should not replace that semi-colon with another character, but instead wrap the string with double quotes (and escape any double quotes that might already be part of the string by doubling them). This is how in CSV syntax you can include such delimiters. In fact, it is not bad practice to wrap such text with double quotes always.

Here is how I'd adapt your loop:

for row in listoflines:
if row.count(';') > 13: # maybe there are even two or more of them
cells = row.split(';')
# Escape double quotes and wrap in double quotes
s = '"' + ';'.join(cells[12:-1]).replace('"', '""') + '"'
# Replace the involved parts with that single part
cells[12:-1] = [s]
# ...and convert back to the string format
row = ';'.join(cells)

finaldocument.append(row)

Matching string between nth occurrence of character in python with RegEx

You could hardcode this:

.*?\/.*?\/.*?\/(.*?)\.

More elegant is something along the lines of this:

(.*?\/){3}(.*?)\.

You can simply change the 3 to suit your pattern. (Note that the group you'll want is $2)

Find substring after nth occurrence of substring in a string in oracle

This will return everything after second occurance of ##:

substr(string, instr(string, '##', 1, 2)+1) 

If you need to find a substring with specific length, then just add third parameter to substr function

substr(string, instr(string, '##', 1, 2)+1, 2) 

You can also use it in query:

select 
substr(some_value, instr(some_value, '##', 1, 2)+1, 2)
from some_table
where...

Get index of nth occurrence of char in a string

Using LINQ to find the index of the 5'th a in the string aababaababa:

var str = "aababaababa";
var ch = 'a';
var n = 5;
var result = str
.Select((c, i) => new { c, i })
.Where(x => x.c == ch)
.Skip(n - 1)
.FirstOrDefault();
return result != null ? result.i : -1;


Related Topics



Leave a reply



Submit