Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to build a Java Array in ABAP

0 Likes
1,370

Hello all,

I have this bit of Java-code ( a function that calculates a distance table to determine the differences between two strings ) and my question is on <b>how this generally</b> would look or <b>be done in abap</b> or <b>more specifically</b>, how can I do the <b>array in abap</b> for this specific need ( as I do not really know the dimensions when defining it ). Is there a simpler approach or function to implement this in abap ?

Thanks in advance!

Clemens

public static int BuildDistanceTable (String s, String t) {
		int d[][]; // matrix
		int n; // length of s
		int m; // length of t
		int i; // iterates through s
		int j; // iterates through t
		char s_i; // ith character of s
		char t_j; // jth character of t
		int cost; // cost
		
		// Step 1
		
		n = s.length ();
		m = t.length ();
		if (n == 0) {
			return m;
		}
		if (m == 0) {
			return n;
		}
		d = new int[n+1][m+1];
		
		// Step 2
		
		for (i = 0; i <= n; i++) {
			d<i>[0] = i;
		}
		
		for (j = 0; j <= m; j++) {
			d[0][j] = j;
		}
		
		// Step 3
		
		for (i = 1; i <= n; i++) {
			
			s_i = s.charAt (i - 1);
			
			// Step 4
			
			for (j = 1; j <= m; j++) {
				
				t_j = t.charAt (j - 1);
				
				// Step 5
				
				if (s_i == t_j) {
					cost = 0;
				}
				else {
					cost = 1;
				}
				
				// Step 6
				
				d<i>[j] = Minimum (d[i-1][j]+1, d<i>[j-1]+1, d[i-1][j-1] + cost);
				
			}
			
		}
		
		// Step 7
		
		return d[n][m];
		}

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,209

ok got it, do one thing take a temp variable of char 1 ,

then pass the charecters of the string one by one to that temp value and append the value to an internal table.clear the temp variable.and do it again,use some do staement for this now how to split the string

temp = string+0(1). gives first char

temp = string+1(1). second

temp = string+2(1). third

etc etc for further details read this stuff

Character Strings in Release 6.10

In Release 6.10 the following new functions are available for character strings:

1. Establishing the length and number of characters

2. New instructions FIND and REPLACE

3. Faster access to strings

4. Support for strings in the database

5. Defining string constants

6. Introduction of string literals

Modification 1

Establishing the length and number of characters

The function CHARLEN provides the length of the first character of a string or of a character-type field.

You can use NUMOFCHAR to obtain the number of characters in a string or a character-type field.

DBMAXLEN provides the maximum length of the string as stored in the ABAP Dictionary.

Modification 2

New instructions FIND and REPLACE

There is a new statement,FIND, for searching in character strings. This replaces the SEARCH command. For replacing characters in character strings the statement REPLACE has been enhanced to include position-based replacement.

Modification 3

Faster access to strings

Offset/length access is now the fastest way to process a string character by character. This technique is also faster than searching in a field of type C that is assigned to a field symbol.

Modification 4

Support for strings in the database

As from Release 6.10, character strings and binary data can be stored in database columns of types STRING or RAWSTRING. The system differentiates between short and long strings:

Short strings consist of a maximum of 256 characters, do not have trailing spaces and can be compared on the database.

Long strings can be of any length and do have trailing spaces; however they cannot be compared on the database.

When working with strings some restrictions have to be observed. Further details are available here.

Modification 5

Defining string constants

Strings can now also be defined as constants and can be given an initial value using the keyword VALUE.

constants: STR1 type string value 'ABC'.

data: STR2 type string value 'XYZ'.

STR2 = STR1.

STR1 = STR2. "Syntax error

write: / STR1, STR2.

Modification 6

Introduction of string literals

String literals are enclosed by grave accents (`)of the form str = `ABC`. String literals are of data type STRING and trailing spaces are taken into account, as opposed to text literals.

data: STR1 type string value 'ABC ',

STR2 type string value `ABC `,

CNT1 type i,

CNT2 type i.

CNT1 = strlen( STR1 ).

CNT2 = strlen( STR2 ).

write: / CNT1, CNT2.

The length for the string STR1 will be CNT1 = 3 and for the string STR2 it will be CNT2 = 5.

10 REPLIES 10
Read only

Former Member
0 Likes
1,209

itabs solve the perpose of arrays as in java or cpp,

please give me the thing you require to do as i cannot understand the java code as such,

thanks waiting for you reply

Read only

0 Likes
1,209

hi vikaas,

what i basically want to do is take two strings and compare them regarding the chars they differ from each other for implementing a simple duplicates check, so this goes further than simple s1 = s2 and check wheter theyre equal or not.

example:

"simon mayer" and "simeon meier" -> 5 differences .

in java, i therefor split the strings in single chars and do fill an 2-dimensional array, at the end i can calculate the number of chars that differ.

now, how could i achieve the same thing in abap - maybe easier ? any idea for that ?

thank you !

Read only

Former Member
0 Likes
1,209

Hi,

I think u can use the concept of internal table in abap.

Regards,

Sruthi

Read only

Former Member
0 Likes
1,210

ok got it, do one thing take a temp variable of char 1 ,

then pass the charecters of the string one by one to that temp value and append the value to an internal table.clear the temp variable.and do it again,use some do staement for this now how to split the string

temp = string+0(1). gives first char

temp = string+1(1). second

temp = string+2(1). third

etc etc for further details read this stuff

Character Strings in Release 6.10

In Release 6.10 the following new functions are available for character strings:

1. Establishing the length and number of characters

2. New instructions FIND and REPLACE

3. Faster access to strings

4. Support for strings in the database

5. Defining string constants

6. Introduction of string literals

Modification 1

Establishing the length and number of characters

The function CHARLEN provides the length of the first character of a string or of a character-type field.

You can use NUMOFCHAR to obtain the number of characters in a string or a character-type field.

DBMAXLEN provides the maximum length of the string as stored in the ABAP Dictionary.

Modification 2

New instructions FIND and REPLACE

There is a new statement,FIND, for searching in character strings. This replaces the SEARCH command. For replacing characters in character strings the statement REPLACE has been enhanced to include position-based replacement.

Modification 3

Faster access to strings

Offset/length access is now the fastest way to process a string character by character. This technique is also faster than searching in a field of type C that is assigned to a field symbol.

Modification 4

Support for strings in the database

As from Release 6.10, character strings and binary data can be stored in database columns of types STRING or RAWSTRING. The system differentiates between short and long strings:

Short strings consist of a maximum of 256 characters, do not have trailing spaces and can be compared on the database.

Long strings can be of any length and do have trailing spaces; however they cannot be compared on the database.

When working with strings some restrictions have to be observed. Further details are available here.

Modification 5

Defining string constants

Strings can now also be defined as constants and can be given an initial value using the keyword VALUE.

constants: STR1 type string value 'ABC'.

data: STR2 type string value 'XYZ'.

STR2 = STR1.

STR1 = STR2. "Syntax error

write: / STR1, STR2.

Modification 6

Introduction of string literals

String literals are enclosed by grave accents (`)of the form str = `ABC`. String literals are of data type STRING and trailing spaces are taken into account, as opposed to text literals.

data: STR1 type string value 'ABC ',

STR2 type string value `ABC `,

CNT1 type i,

CNT2 type i.

CNT1 = strlen( STR1 ).

CNT2 = strlen( STR2 ).

write: / CNT1, CNT2.

The length for the string STR1 will be CNT1 = 3 and for the string STR2 it will be CNT2 = 5.

Read only

0 Likes
1,209

Vikaas,

thanks for the enligthenment.

I just tried this:


  DATA:
    s type string value ` abc `,
    l type i,
  l = NUMOFCHAR( s ). "=> 4
  l = STRLEN( s ).    "=> 5 

I do not understand the NUMOFCHAR function. Does it not count trailing blanks?

But I'm happy text processing in ABAP finally is going to be fun.

Regards,

Clemens

Read only

0 Likes
1,209

ABAP_KEY&NUMOFCHAR

Returns the number of characters of a character-type field. In Unicode systems and non-Unicode systems with single-byte codepages, this function behaves like STRLEN. In non-Unicode systems with double-byte codepages, characters that take up more than one byte are counted with length 1, while STRLEN returns length 2.

Thanks and reward point if of any use

Read only

0 Likes
1,209

The string has 3 non-blank characters and 2 spaces. I'd expect results as 3 or 5. Whre does the 4 come from?

Regards,

Clemens

Copying the online help does not help if it does not explain this.

Read only

0 Likes
1,209

Hi Clemens,

I think the result would be 4 in both cases.

Both strlen and numofchar don't count the trailing spaces. (Counts the leading space though).

Regards,

Ravi

Read only

0 Likes
1,209

Hi Ravi,

as you may have noticed, I used back hyphens (`, french accent grâve) to enclose the string value. I know that this will not remove the leading and trailing spaces i.e. using concatenate. So I expected the result as 5 in both cases. Still the difference between NUMOFCHAR and STRLEN is not clear.

Regards,

Clemens

Read only

0 Likes
1,209

Hi to all,

this is the solution that i found:


PERFORM dist USING string1 string2.

FORM dist USING p1 p2 TYPE string.


  TYPES:
  Begin OF str_array,
        str1 TYPE string,
        str2 TYPE string,
        END OF str_array.

  DATA: l_str_array TYPE str_array.

  DATA: l_tab_array TYPE STANDARD TABLE OF str_array.


  data: temp1(1) TYPE c,
        temp2(1) TYPE c,
        flag TYPE i,
        string_temp TYPE string,
        s1 TYPE string,
        s2 TYPE string,
        s1_length TYPE i,
        s2_length TYPE i,
        n1 TYPE i,
        n2 TYPE i,
        s1_i TYPE c,
        s2_i TYPE c,
        count TYPE i,
        dupl TYPE p DECIMALS 2.

  s1 = p1.
  s2 = p2.

  s1_length = strlen( s1 ).
  s2_length = strlen( s2 ).


  IF s1_length lt s2_length.

       string_temp = s1.
       s1 = s2.
       s2 = string_temp.

       s1_length = strlen( s1 ).
       s2_length = strlen( s2 ).
       flag = 1.

  ENDIF.


  IF s1_length ge s2_length.
    
       

    WHILE n1 ne s1_length.


      temp1 = s1+n1(1).


      IF n1 lt s2_length.
        temp2 = s2+n1(1).

      ELSE.
        temp2 = ''.
       count = count - 1.
      ENDIF.

      IF temp1 ne temp2.
        count = count + 1.
      ENDIF.


      IF flag = 0.
      l_str_array-str1 = temp1.
      l_str_array-str2 = temp2.
      ELSE.
      l_str_array-str1 = temp2.
      l_str_array-str2 = temp1.
      ENDIF.

      APPEND l_str_array TO l_tab_array .


      n1 = n1 + 1.

    ENDWHILE.


    LOOP AT l_tab_array INTO l_str_array.

      WRITE: l_str_array-str1.

    ENDLOOP.

    ULINE.

    LOOP AT l_tab_array INTO l_str_array.
      WRITE: l_str_array-str2.
    ENDLOOP.

    dupl = 100 - ( count * 100 ) / s1_length.
    WRITE: (20)dupl,' %'.

ENDIF.

ENDFORM.