Jea
Jea

Reputation: 484

Replacing the first two commas of a string

Is there a regex that can be used for replacing only the two first commas in a String with another character?

The reason why I need this is that I am working with RDF triples, where I need to handle the subject, predicate and object separately. So I split them into three separate parts using the String.split() method.

This is an example of the Strings that I need to split:

[http://dbpedia.org/resource/Tom_Mathisen, http://www.w3.org/2000/01/rdf-schema#comment, "Tom Mathisen (born 14 August 1952) is a Norwegian comedian, actor, screenwriter and musician. He usually handles bass, guitar and vocals. He was in the comedy group Prima Vera from 1976 to 1983 along with Jahn Teigen and Herodes Falsk. Later he continued to work with Herodes Falsk on many TV/scene shows and records."@en]

For instance I want this String to look like this instead, so I can split using $:

[http://dbpedia.org/resource/Tom_Mathisen$ http://www.w3.org/2000/01/rdf-schema#comment$ "Tom Mathisen (born 14 August 1952) is a Norwegian comedian, actor, screenwriter and musician. He usually handles bass, guitar and vocals. He was in the comedy group Prima Vera from 1976 to 1983 along with Jahn Teigen and Herodes Falsk. Later he continued to work with Herodes Falsk on many TV/scene shows and records."@en]

I did it in a less elegant way:

StringBuilder stringbuilder = new StringBuilder(str);
int commaCounter = 0;
for (int i = 0; i < stringbuilder.length(); i++) {
    if (stringbuilder.charAt(i) == ',') {
        stringbuilder.setCharAt(i, '$');
        commaCounter++;
        if (commaCounter == 2) {
            break;
        }
    }
}
str = stringbuilder.toString();

Upvotes: 1

Views: 605

Answers (5)

Avinash Raj
Avinash Raj

Reputation: 174844

Solution based on regex,

System.out.println("a, b, c, d".replaceAll("^([^,]*),([^,]*),", "$1X$2X"));

Output:

aX bX c, d

DEMO

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35587

You can split your text from fist two , directly as follows. To split no need to replace , with $. That is overkill just split at once from first two ,;

String str="http://dbpedia.org/resource/Tom_Mathisen, http://www.w3.org/2000/01/rdf-schema#comment, \"Tom Mathisen (born 14 August 1952) is a Norwegian comedian, actor, screenwriter and musician. He usually handles bass, guitar and vocals. He was in the comedy group Prima Vera from 1976 to 1983 along with Jahn Teigen and Herodes Falsk. Later he continued to work with Herodes Falsk on many TV/scene shows and records.\"@en";
String[] arr=str.split(",",3);
 for (String i : arr) {
        System.out.println(i);
 }

Out put:

http://dbpedia.org/resource/Tom_Mathisen
http://www.w3.org/2000/01/rdf-schema#comment
"Tom Mathisen (born 14 August 1952) is a Norwegian comedian, actor, screenwriter and musician. He usually handles bass, guitar and vocals. He was in the comedy group Prima Vera from 1976 to 1983 along with Jahn Teigen and Herodes Falsk. Later he continued to work with Herodes Falsk on many TV/scene shows and records."@en

Upvotes: 1

kai
kai

Reputation: 6887

Use two times replaceFirst. This will do what you want. Use a forloop if you want to replace an specific char the first n times.

    String s = "this, is, a, Test";
    int count = 2;
    for(int i = 0; i < count; i++)
        s = s.replaceFirst(",", "");
    System.out.println(s);

Output: this is a, Test

Or if you always want to replace only the first two chars you can chain the methods.

   String s = "this, is, a, Test";
   s = s.replaceFirst(",", "$").replaceFirst(",", "$");

Upvotes: 3

DeiAndrei
DeiAndrei

Reputation: 947

If you only want to replace the comma, you can call replaceFirst 2 times. For example:

    String someString = "a, b, c, d";
    someString = someString.replaceFirst(",", "-");
    someString = someString.replaceFirst(",", "-");

    System.out.println(someString);

Output is:

    a- b- c, d

Upvotes: 1

vikingsteve
vikingsteve

Reputation: 40438

Something like this is relatively straight forward?

String s = input.replaceFirst(",", "x").replaceFirst(",", "x");

Upvotes: 6

Related Questions