How to reverse a string using arrays in oracle?

Member

by domenico , in category: MySQL , 9 months ago

How to reverse a string using arrays in oracle?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 9 months ago

@domenico 

To reverse a string using arrays in Oracle, you can use PL/SQL code. Here is an example of how you can achieve this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
DECLARE
   str VARCHAR2(100) := 'Hello World';
   str_length NUMBER := LENGTH(str);
   reversed_str VARCHAR2(100);
   i NUMBER := 1;
   arr dbms_sql.VARCHAR2A;
BEGIN
   -- Convert string to array
   FOR j IN 1..str_length LOOP
      arr(j) := SUBSTR(str, j, 1);
   END LOOP;

   -- Reverse the array
   FOR j IN REVERSE 1..str_length LOOP
      reversed_str := reversed_str || arr(j);
   END LOOP;

   dbms_output.put_line('Original String: ' || str);
   dbms_output.put_line('Reversed String: ' || reversed_str);
END;
/


In this code snippet, we first declare a string str and then convert it to an array arr. We then loop through the array in reverse order and concatenate the elements to form the reversed string reversed_str. Finally, we output the original and reversed strings using dbms_output.put_line.


You can run this PL/SQL code in an Oracle database to reverse a given string using arrays.

Related Threads:

How to select a table using a string in oracle?
How to split a string in oracle using substr/instr?
How To Calculate Parabolic SAR (Stop and Reverse) using Visual Basic?
How To Calculate Parabolic SAR (Stop and Reverse) using Lua?
How to replace string before string using regex?
How to create json files from 3 php arrays?