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

find the position of character in a string

137 100+
hi all,

how could i find the corresponding positions on a string which have two or more same characters
i mean
select position('a' in 'abcdefedcba')
it should return 1 and 11 simultaneoulsy

but above query returns 1 only. how could i get the next position value.
Jun 1 '08 #1
4 18963
rski
700 Expert 512MB
hi all,

how could i find the corresponding positions on a string which have two or more same characters
i mean
select position('a' in 'abcdefedcba')
it should return 1 and 11 simultaneoulsy

but above query returns 1 only. how could i get the next position value.
Maybe like that

Expand|Select|Wrap|Line Numbers
  1. >select * from (select case when (string_to_array(trim(trailing '.' from regexp_replace('abcdefedcba','(.)','\\1.','g')),'.'))[i]='a' then i end as count from generate_series(1,length('abcdefedcba')) i) foo where count is not null;
  2.  count
  3. -------
  4.      1
  5.     11
  6. (2 rows)
  7.  
In this query i assume that there are no '.' character in the string. If there is '.' you must use another character in here
Expand|Select|Wrap|Line Numbers
  1. select case when (string_to_array(trim(trailing '.' from regexp_replace('abcdefedcba','(.)','\\1.','g')),'.')
  2.  
Another solution is to write a function doing that. Should be simple to do. If you want I can write it here
Jun 1 '08 #2
coolminded
137 100+
Maybe like that

Expand|Select|Wrap|Line Numbers
  1. >select * from (select case when (string_to_array(trim(trailing '.' from regexp_replace('abcdefedcba','(.)','\\1.','g')),'.'))[i]='a' then i end as count from generate_series(1,length('abcdefedcba')) i) foo where count is not null;
  2.  count
  3. -------
  4.      1
  5.     11
  6. (2 rows)
  7.  
In this query i assume that there are no '.' character in the string. If there is '.' you must use another character in here
Expand|Select|Wrap|Line Numbers
  1. select case when (string_to_array(trim(trailing '.' from regexp_replace('abcdefedcba','(.)','\\1.','g')),'.')
  2.  
Another solution is to write a function doing that. Should be simple to do. If you want I can write it here
hi,
thanx for the reply,
but it didn't work in my PgAdmin. It shows nothing. i'm using PostgreSQL 8.1.3. does it depend on the version too??

and it will be very kind if you write a code or a function for doing that. i'll be very thankful to you.
coolminded
Jun 2 '08 #3
rski
700 Expert 512MB
>select * from (select case when (string_to_array(trim(trailing '.' from regexp_replace('abcdefedcba','(.)','\\1.','g')),'. '))[i]='a' then i end as count from generate_series(1,length('abcdefedcba')) i) foo where count is not null;
count
-------
1
11
(2 rows)

sorry my mistake a put a space after '.' now it should work, i'll send you a function just when i find some free time to write it.
Jun 2 '08 #4
rski
700 Expert 512MB
The function definition may look like that
Expand|Select|Wrap|Line Numbers
  1. create or replace function pss(text,text) returns setof integer as
  2. $rski$
  3. declare
  4. s int;
  5. begin
  6. for i in 0..length($1) loop
  7.         if substr($1,i,1)=$2 then
  8.                 return next i;
  9.         end if;
  10. end loop;
  11. end;
  12. $rski$
  13. language plpgsql
  14.  
and you can use it in that way
Expand|Select|Wrap|Line Numbers
  1. >select * from pss('alabama','a');
  2.  pss
  3. -----
  4.    1
  5.    3
  6.    5
  7.    7
  8. (4 rows)
  9.  
Jun 2 '08 #5

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

Similar topics

9
by: PaulThomas | last post by:
I want to "look" through a comma delimited String and "take it apart" by finding the comma's and then put each "set of characters" into seperate strings - - - maybe an array, maybe seperate cells...
2
by: Looks Like Trent | last post by:
Find a character in a string, at a specified position. How do you do this? Any help will be appreciated....
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
2
by: Faz | last post by:
I am trying to extract text before a certain character appears in a string. This character is the letter "C". Here is some sample data - the field is called REFERENCE_2: REFERENCE_2...
4
by: Neil Robbins | last post by:
Could anyone suggest a way in which I could find the position of the cursor within a textbox. I want to evaluate every keypress made within the textbox in order to validate it but in order to do...
6
by: KJ | last post by:
How can you look for a character in a certain position with VB.Net and if the string length is less than the position have it return and empty string instead of an error? Substring returnns an...
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...
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
7
kireytir
by: kireytir | last post by:
hi all, this is my code (in interrupt function) for finding character in string (data). Ekle_text.Text += data; string veri = Ekle_text.Text; int found = veri.IndexOf("x"); if (found !=...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.