How can i split string in java using combine special characters -
how can split string
using combine special characters?
for example, if combine special characters {@}
:
string str = "this test string1.{@}this test string2@(#*$ ((@@{}"; stringtokenizer stoken = new stringtokenizer(str, "\\{\\@\\}"); while (stoken.hasmoreelements()) { system.out.println(stoken.nextelement()); }
what expect above program :
this test string1. test string2@(#*$ ((@@{}
you can not use characters special meanings : \ ^ $ . | ? * + ( ) [ { }, think there 12 of them. therefore change character split string "/-/"
the code looks 1 underneath:
public class splitstring { public static void main(string[] args) { string s = "this test string1./this test string2@(#*$ ((@@{}"; string[] fragments = s.split("/"); string fragment1 = fragments[0]; string fragment2 = fragments[1]; system.out.println(fragment1); system.out.println(fragment2); } }
this solution based on answer in thread: stackoverflow split string
Comments
Post a Comment