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

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -