How to Find Nth Occurrence of Character in a String

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.

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')

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)

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

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);

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)

SQL Server - find nth occurrence in a string

One way (2k8);

select 'abc_1_2_3_4.gif  ' as img into #T
insert #T values ('zzz_12_3_3_45.gif')

;with T as (
select 0 as row, charindex('_', img) pos, img from #T
union all
select pos + 1, charindex('_', img, pos + 1), img
from T
where pos > 0
)
select
img, pos
from T
where pos > 0
order by img, pos

>>>>

img pos
abc_1_2_3_4.gif 4
abc_1_2_3_4.gif 6
abc_1_2_3_4.gif 8
abc_1_2_3_4.gif 10
zzz_12_3_3_45.gif 4
zzz_12_3_3_45.gif 7
zzz_12_3_3_45.gif 9
zzz_12_3_3_45.gif 11

Update

;with T(img, starts, pos) as (
select img, 1, charindex('_', img) from #t
union all
select img, pos + 1, charindex('_', img, pos + 1)
from t
where pos > 0
)
select
*, substring(img, starts, case when pos > 0 then pos - starts else len(img) end) token
from T
order by img, starts

>>>

img starts pos token
abc_1_2_3_4.gif   1 4 abc
abc_1_2_3_4.gif   5 6 1
abc_1_2_3_4.gif   7 8 2
abc_1_2_3_4.gif   9 10 3
abc_1_2_3_4.gif   11 0 4.gif  
zzz_12_3_3_45.gif 1 4 zzz
zzz_12_3_3_45.gif 5 7 12
zzz_12_3_3_45.gif 8 9 3
zzz_12_3_3_45.gif 10 11 3
zzz_12_3_3_45.gif 12 0 45.gif

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)


Related Topics



Leave a reply



Submit