I an facing one very strange issue while getting string value from a bean and split it using pattern :
I have to get the value and split it on the basis of "\n" presenting in String. I am doing as follows :
String attributeName = (attributeList.get(i).getName());
Pattern pattern = Pattern.compile("\n");
String formatArray1 = pattern.split(attributeName,0);
Problem : THe split is not working
But, If the same string value is hardcoded then split works perfectly fine as follows :
If the split method works differently on an unknown string versus a known string, that can only mean that the two strings are different. It would be extremely easy to put in some debugging code to find out the actual value of the unknown string, so I suggest you should start by doing that.
posted 6 years ago
Thanks for your reply..
I have already debugged the code and also put SOP to print the value getting from list.. Copied the same value (LOC_DATA_CURRENCY\nMX EQ (EUR)\nAAL\n Gross Loss) from console and hardcoded in String variable.
But still split is working fine with hardcoded String variable but, not with the value getting from list.... This is really strange behavior......
soni lal wrote:Copied the same value (LOC_DATA_CURRENCY\nMX EQ (EUR)\nAAL\n Gross Loss) from console and hardcoded in String variable.
But the "\n" part you copied from the console is two characters, the backslash character and the "n" character. When you put that into a String literal it is interpreted as one character, the new-line character. So, like I said, the strings are different.
You should have seen that difference when you displayed the two strings. Perhaps your debugging didn't include that?
posted 6 years ago
Thanks for the reply!!
Yes you are right .... while displaying both the string one is getting printed as complete value i.e "LOC_DATA_CURRENCY\nMX EQ (EUR)\nAAL\n Gross Loss" ( I think \n is getting treated as two separate characters not as single i.e line separator)
if the same is hardcoded then the string is getting printed as :
MX EQ (EUR)
Now, what I have to do to split the string on basis of "\n" ...
Sounds to me like the string to be split does not contain '\n' (the new line characters) but instead contains the two characters '\' followed by 'n'. In which case one needs to split based on "\\\\n" (the four '\' are needed because both Java and regex use '\' as an escape character) .
Lasagna is spaghetti flvored cake. Just like this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!