java - Why string.matches("^[A-Z0-9\\-\\_]+") not working when i am giving value of string by manipulating another string, -


string astr="test-1-tv_50"; system.out.println(astr.matches("^[a-z0-9\\-\\_]+")); //true. 

but why not working..?

string astr1= "$local:test12-1-tv_50 xs:boolean";  int strtindex=astr.indexof(":"); int endindex=astr.indexof("as");  string extractedstr=astr1.substring(strtindex+1,endindex);  //test12-1-tv_50   system.out.println(extractedstr.matches("^[a-z0-9\\-\\_]+")); //false.  

why giving result false.???

there's trailing space in extractedstr.

so contains "test12-1-tv_50 " (not there's space after final 0).

you can either replace endindex astr.indexof(" as") (starting space) or call trim() on extractedstr:

string extractedstr=astr1.substring(strtindex+1,endindex).trim(); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -