protected String parseString ( String v ) { /* * Parsing a string requires several steps to be taken. First, we need * to strip quotes from the ends of the string. */ v = v . substring ( 1 , v . length ( ) - 1 ) ; StringBuffer result = new StringBuffer ( ) ; // Second, step through the string and replace escaped characters for ( int i = 0 ; i < v . length ( ) ; i ++ ) { if ( v . charAt ( i ) == ' ' ) { if ( v . length ( ) <= i + 1 ) { throw new RuntimeException ( "unexpected end-of-string" ) ; } else { char replace = 0 ; int len = 2 ; switch ( v . charAt ( i + 1 ) ) { case ' ' : replace = ' ' ; break ; case ' ' : replace = ' ' ; break ; case ' ' : replace = ' ' ; break ; case ' ' : replace = ' ' ; break ; case ' ' : replace = ' ' ; break ; case ' ' : replace = ' ' ; break ; case ' ' : replace = ' ' ; break ; case ' ' : replace = ' ' ; break ; case ' ' : len = 6 ; // unicode escapes are six digits long, // including "slash u" String unicode = v . substring ( i + 2 , i + 6 ) ; replace = ( char ) Integer . parseInt ( unicode , 16 ) ; // unicode i = i + 5 ; break ; default : throw new RuntimeException ( "unknown escape character" ) ; } result = result . append ( replace ) ; i = i + 1 ; } } else { result = result . append ( v . charAt ( i ) ) ; } } return result . toString ( ) ; }
Parse a string constant whilst interpreting all escape characters .