Quick Q

Associate
Joined
23 Aug 2004
Posts
1,493
s(100001740,1,'entity',n,1,11).

I've got a document which I need to read in Java. I've created a file reader to split the string on the ",".

My question, is there an easy way to remove the s(, the ' ' and the ).?
 
Here is a quick and easy way of doing it. There may be a better way but I'm not great with regular expressions. Basically this uses the String operations 'replace(regex, String)' and 'replaceAll(regex, String)' to replace the specified characters with nothing ("").

Code:
String str = "s(100001740,1,'entity',n,1,11).";
str = str.replace("s(","");
str = str.replace(").","");
str = str.replaceAll("'","");
System.out.println(str);

// Output:
// 100001740,1,entity,n,1,11

If you wanted to split it up aftwards and use the bits you could do something like:

Code:
String[] splitstr = str.split(",");
     for (String s : splitstr) {
          // Do something useful with the bits here
          System.out.println(s);
     }

// Output:
// 100001740
// 1
// entity
// n
// 1
// 11

Of course if you wanted to split the string first then remove the garbage then you can change the code around accordingly, running the replace operations on the fragments of the split string.
 
Last edited:
Back
Top Bottom