on 04-16-2009 3:20 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
int length = a.length();
if (length>48) {
String b=a.substring(7,48);
return b;
}
else
return a;
try that
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
You can use standard function "Substring" from 7 to 48.
Regards,
Chandra.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi
put substring (8,48)
rgds
srini
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
73 | |
26 | |
10 | |
9 | |
7 | |
6 | |
4 | |
4 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.