regex - New line character representation in Java -
this question has answer here:
- how split java string @ backslash 6 answers
i have field in database looks this
abcd.\nefgh
on java side, variable (message), , need split variable on 2 parts: first , second.
something this:
message="abcd.\nefgh" ;//this not happens ??? first = //anything before '\n' second = //anything after '\n'
i tried following
if(message.indexof('\n')>-1){ first = message.substring(0, message.indexof('\n')); second = message.substring(message.indexof('\n')) ; }
however, code never enters body of if statement. checked debugger, , reveals message variable indeed
abcd.\\nefgh
so, tried use of string.split method,
string rabit = "abcd\\nefgh" ; system.out.println("results \n "+ rabit.split("\n")) ; //1 //and alternative system.out.println("results \n "+ rabit.split("\\n")) ; //2
however, here problem. in both, 1 , 2 outputs, array 1 element. - full message. pattern not found.
so, can - problem here? fact variable message comes database \n? other suggestions? how can resolve ?
if string contains literal \n
need four escape characters in split
delimiter regex:
rabit.split("\\\\n")
Comments
Post a Comment