1

I have comma separated string variable like:

String doctors = "doc_vijayan,doc_zubair,doc_Raja" 

But i want to delete "doc_" from the above String and First Letter should display in capital. I need output like this:

String doctors1 = "Vijayan, Zubair, Raja"

How to do that?

1

5 Answers 5

5

You can try that :

public String splitDoctors(String doctorsString){
    String[] doctors = doctorsString.split(",");
    boolean isFirst = true;
    StringBuilder sb = new StringBuilder();
    for(String doctor : doctors){
        if(!isFirst){
            sb.append(", ");
        }else{
            isFirst = false;
        }
        sb.append(doctor.substring(4,5).toUpperCase());
        sb.append(doctor.substring(5));
    }
    return sb.toString();
}
Sign up to request clarification or add additional context in comments.

3 Comments

If you use this method, it may be a good idea to use if (doctor.startsWith("doc_")) ... to check if it, in fact, starts with the given string.
i am facing one issue with above code. first string is displayed twice like: String doctors1 = "VijayanVijayan, Zubair, Raja"
What is the string you use in parameter ?
3

Guava

With Guava, you can write something like this:

    import com.google.common.base.*;
    import com.google.common.collect.*;
    //...

    String doctors = "doc_vijayan,doc_zubair,doc_Raja";

    Function<String,String> chopAndCap =
        new Function<String,String>() {
            @Override public String apply(String from) {
                return from.substring(4, 5).toUpperCase()
                    + from.substring(5);
            }           
        };

    Iterable<String> docs = Splitter.on(',').split(doctors);
    docs = Iterables.transform(docs, chopAndCap);
    doctors = Joiner.on(", ").join(docs);

    System.out.println(doctors);
    // Vijayan, Zubair, Raja

So the concrete logical steps are:

  • Define a Function to perform the chop-and-cap
  • Use Splitter to split into Iterable<String>
  • Iterables.transform each element using the above Function
  • Use Joiner to join from the transformed Iterable back to a String

If you're comfortable with this kind of programming style, you can just assemble the entire process into one smooth operation:

    System.out.println(
        Joiner.on(", ").join(
            Iterables.transform(
                Splitter.on(',').split(doctors),
                new Function<String,String>() {
                    @Override public String apply(String from) {
                        return from.substring(4, 5).toUpperCase()
                            + from.substring(5);
                    }           
                }
            )
        )
    );
    // Vijayan, Zubair, Raja

Apache Commons Lang

With Apache Commons Lang, you can write something like this:

    import org.apache.commons.lang.*;
    //...

    String doctors = "doc_vijayan,doc_zubair,doc_Raja";

    String[] docs = StringUtils.split(doctors, ',');
    for (int i = 0; i < docs.length; i++) {
        docs[i] = StringUtils.capitalize(
            StringUtils.substringAfter(docs[i], "doc_")
        );
    }
    doctors = StringUtils.join(docs, ", ");
    System.out.println(doctors);
    // Vijayan, Zubair, Raja

Note that you if you have both Guava and Apache Commons Lang, you can use StringUtils.capitalize/substringAfter in chopAndCap function above.

Comments

1
final String[] docs = doctors.split(",");
final StringBuilder sb = new StringBuilder();
for (final String d : docs) {
   String doct = d.replace("doc_", "");
   doct = doct.subString(0,1).toUpperCase() +  doct.subString(1).toLowerCase();
   sb.append(sb.length() > 0 ? ", " : "");
   sb.append(doct);
} // you should be able to do the rest.

Comments

1
String regex = "doc_.";
Matcher matcher = Pattern.compile(regex).matcher(doctors);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
    String group = matcher.group();
    int i = group.length() - 1;
    matcher.appendReplacement(sb, String.valueOf(group.charAt(i)).toUpperCase());
}
matcher.appendTail(sb);
System.out.print(sb.toString());

Comments

0

The easiest way is to use 'any' StringUtils library out there. For example org.apache.commons.lang.StringUtils has a chomp and a capitalize method which should bring you a long way.

2 Comments

can u please elaborate how to use StringUtil for this?
Sure, i've looked it up, you code make it into a oneliner like this if you use apache commons lang library: String result = WordUtils.capitalizeFully(StringUtils.chomp(doctors, "doc"), new char[] {','});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.