aspose file tools*
The moose likes Java in General and the fly likes Regarding String manipulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding String manipulation" Watch "Regarding String manipulation" New topic
Author

Regarding String manipulation

arav vara
Greenhorn

Joined: Feb 10, 2010
Posts: 6
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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39


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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
arav vara
Greenhorn

Joined: Feb 10, 2010
Posts: 6
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

Joined: Oct 13, 2005
Posts: 36494
    
  16
Too difficult for "beginning". Moving thread.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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

Joined: Feb 10, 2010
Posts: 6
Thanks a lot. How do i close this question and give points for accepted answer?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

arav vara wrote:Thanks a lot. How do i close this question and give points for accepted answer?


Click the resolve button.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding String manipulation
 
Similar Threads
Regarding split method
StringSplit regex question: missing part of my string?
escaping html characters and a string starting with double quote
Removing single quotes from the string
How to use trim() with String.split(regx)?