Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in finding " \' " in String or StringBuffer.  RSS feed

 
Paresh Joshi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
in my program my need is to find \' in a string which i am reading from file.
now problem is java considering "\" as an escape char.
i cant change contents of file manually from "\'" to "\\'"

Help :
can someone help me in finding that.
or
is there anyway to off the escape char or to change escape character.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16006
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you are confused about Java considering \ as an escape character.

It is only the Java compiler that considers \ as an escape character in Java source code. The Java compiler does not have anything to do with the file your program is reading which contains the \ character. For your program, a \ in the input file is just like any other regular character; it's not handled as an escape character somehow.

You only need to write \\ instead of \ in the source code of your program. You do not need to change the file that your program is reading.

Show us your code and explain why it isn't doing exactly what you want it to do.
 
michael warren
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Copied from locked thread in intermediate forum, just to save anyone recreating what I've put below - Think Jesper is probably right)
Not sure where you're going wrong , but it works on my machine
Are you sure String st contains what you think it contains ?

I just tried it out using the following class, the file test.txt contains one line as followsinsert into raexceptionlog (ErrorMsg) values ('The valid combination is \'R\' & \'-\' OR \'D\')

The result is
72
Pos found in file is 72

Note that if you're building the String up in the source code as in String st below you need the escape characters.

code:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!