cancel
Showing results for 
Search instead for 
Did you mean: 

extract portion of the string ?

Former Member
0 Kudos

Hi Guys,

My string field in the response should not have more than 40 char. The string i am intrested from the source is from 8th charachter to 40 char. I am using the following code and getting only 32 chars. I need 40 char.

int length = a.length();

if (length>40) {

String b=a.substring(8,40);

return b;    
   } 

else 

return a;

any help would be appreciated

Thanks,

Raj

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

this is the code to fullfill ur requirement


int length = a.length();
String temp=null,b=null;
int tempLen;
 
if (length>40) {
 
     temp=a.substring(8,length);
     tempLen = temp.length();
              if(tempLen> 40)
                 {
                     b= temp.substring(0,39);
                    return b;    
                } 
             else
            return temp;
}
 
else 
 
return a;

<h5> in this code if the length is less than 40 return the string as it is otherwise get the substring from 9 char to the end of the string then check its length if it is greater than 40 then get only the 40 characters

Former Member
0 Kudos

Hi Kubra,

Thanks for the help. It worked perfectly fine as required and i appreciate your help.

Thanks for Manish as well. I appreciate all.

Raj

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi!

I think Manisha gave the right tip. If you always need a return value with an exact length of 40 bytes regardless of the real length of string a, then you have to ....

lenghten the string, if length of a is < 40 bytes

cut the string, if length of a is > 40 bytes.

Regards,

Volker

Former Member
0 Kudos

i guess ..u have understood what i want to convey u.

Rgds,

Manisha

Former Member
0 Kudos

Hi Manisha,

I have understood what you said.

If the string length is < 40 then i just map and send it in the response.

I am worried only if it is more than 40 chars as the receiver system cannot handle more than 40 chars.

I see all the response messages in the case of failures has more than 40 chars and in all the message i dont want the first 8 chars and i want to have the rest but should not exceed 40 and if it has less than 40 also no problem.

can you please help with the Java code

Thanks,

Raj

Shabarish_Nair
Active Contributor
0 Kudos
int length = a.length();
 
if (length>48) {
 
String b=a.substring(7,48);
 
return b;    
   } 
 
else 
 
return a;

try that

Former Member
0 Kudos

Hi Shabrish,

I combined your code together and it works but i am not sure whether the code is good.

int length = a.length();

if (length>40) {

if (length>48) {

String b=a.substring(8,48); 

return b;        
                   }

else  {

String c = a.substring(8, length);

return c;
        }
                
    }

  } 

else 

return a;

Thanks,

Raj

Edited by: raj reddy on Apr 16, 2009 8:24 PM

Former Member
0 Kudos

You can re-write same code in diff. way..

How abt...

if (a.length()>48) {

return a.substring(8,48);

} else {

return a.substring(8,40);

}

Hope this will help.

Thanks,

Nilesh

Former Member
0 Kudos

Hi Nilesh,

It is throwing index out of bounds if <48 and >40 as the else part is taking only 32.

Thanks,

Raj

Former Member
0 Kudos

Hi,

1...

If ur sending string of length <48 and >40 Then substring u retreive cannot be of 40 char so u need to perfix it.

So

String b;

int length = a.length();

if (length>48) {

b=a.substring(8,48);

}

else

{

int cnt = 48 - length;

for(int i=0; i < cnt; i)

+b = b + " "; // one blank space+

+b = b + a.substring(8,length);+

return b;

The above logic will work even if u r sending string <40 char as it is calculating in cnt to make it 40 char

2... if i assume tht string which u r going to send will be always of 40char(fixed length) then u have to prefix with it..no other option to make it again 40char.

Let me know what is ur exact requirment.

Regards,

Manisha

Former Member
0 Kudos

Hi,

As u r specifying a.substring(8,40) in ur code..it will always give u 32 char only and not 40 char.

as u have put condition as if(length>40) it can be 41 also and in that case also u will not get 40 char as a string as from index-8 to index-41, it is just 33 char.

here if string length is less than 48 then i guess u need to prefix or sufix with something to get length as 40 char staring from index-8 and if length is 48 char or more then u can have 40 char string by puting a.substring(8,48). here u need to modify ur condition and need to add code for prefix or sufix

Regards,

Manisha

former_member581827
Participant
0 Kudos

Hi,

You can use standard function "Substring" from 7 to 48.

Regards,

Chandra.

Former Member
0 Kudos

hi

put substring (8,48)

rgds

srini