すべてのコンマを、Java の二重引用符内にある特殊文字 (「#」など) に置き換えたいと考えています。
以下は文字列です:
String line="\"Lee, Rounded, Neck, Printed\",410.00,300.00,\"Red , Blue\",lee";
出力:
"Lee# Rounded# Neck# Printed",410.00,300.00,"Red # Blue",lee
私はこれを試しました:
public class Str {
public static void main(String[] args) {
String line="\"Lee, Rounded, Neck, Printed\",410.00,300.00,\"Red , Blue\",lee";
String lineDelimiter=",";
String templine=line;
if(templine!=null && templine.contains("\""))
{
Pattern p=Pattern.compile("(\".*?"+Pattern.quote(lineDelimiter)+".*?\")");
Matcher m=p.matcher(templine);
if(m.find())
{
for (int i = 1; i <= m.groupCount(); i++) {
String Temp=m.group(i);
String Temp1=Temp;
Temp=Temp.replaceAll("(,)", " ## ");
line=line.replaceAll(Pattern.quote(Temp1),Pattern.quote(Temp));
}
}
}
}
}
上記のコードを使用すると、引用符内に存在する文字列の最初の出現のみを見つけることができ、2番目の文字列(「赤、青」)ではありません。