Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding String manipulation

 
arav vara
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the string "MO""RET" gets stored in items[1] array after the split command. After it get's stored I do a replaceall on this string and it replaces all the double quotes.
But I want it to be stored as MO"RET. How do i do it. In the csv file from which i process using split command Double quotes within the contents of a Text field are repeated (Example: This account is a ""large"" one"). So i want retain the one of the two quotes in the middle of string if it get's repeated . How can i do it?

String items[] = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
items[1] has "MO""RET"
String recordType = items[1].replaceAll("\"","");
After this recordType has MORET. I want it to have MO"RET
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Use a negative look ahead, to only replace quotes that isn't followed by another quote.

Henry
 
arav vara
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. In case if the my string has a value of "TEST"REPLA". If there is only one single double quote in the middle of the string how can i delete the first ,last quote and retain all the middle quote. I want the output as TEST"REPLA . I dont want to use the split.

Example 2 : "EXAM"PLE"2IN" I want the output as EXAM"PLE"2IN First and last quotes needs to be deleted
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for "beginning". Moving thread.
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arav vara wrote:
Example 2 : "EXAM"PLE"2IN" I want the output as EXAM"PLE"2IN First and last quotes needs to be deleted


Of course, this is different from the your previous request.... The obvious solution (that will handle your previous request along with this addition) would be a regex using logical or operators, and positive look ahead for the replace all -- meaning beginning followed by quote OR quote followed by end OR quote followed by quote (with look ahead).

Henry
 
arav vara
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. How do i close this question and give points for accepted answer?
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arav vara wrote:Thanks a lot. How do i close this question and give points for accepted answer?


Click the resolve button.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic