java - String.replaceAll for multiple characters -
java - String.replaceAll for multiple characters -
i have line ^||^
delimiter, using
int charcount = line.replaceall("[^" + fileseperator + "]", "").length(); if(fileseperator.length()>1) { charcount=charcount/fileseperator.length(); system.out.println(charcount+"char count between"); }
this not work if have line has stray |
or ^
counts these well. how can modify regex or other suggestions?
if understand correctly, you're trying count number of times ^||^
appears in string.
if that's case, can use:
matcher m = pattern.compile(pattern.quote("^||^")).matcher(line); int count = 0; while ( m.find() ) count++; system.out.println(count + "char count between");
but don't need regex engine this.
int startindex = 0; int count = 0; while ( true ) { int newindex = line.indexof(filedelimiter, startindex); if ( newindex == -1 ) { break; } else { startindex = newindex + 1; count++; } }
java
Comments
Post a Comment