views:

148

answers:

5
+1  Q: 

Java String Method

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?

+2  A: 
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.
fastcodejava
You still need to add the comma and a space between the names...
AndHeCodedIt
subString(0,1) is not taking.....
Gnaniyar Zubair
+5  A: 

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();
}
Colin Hebert
thanks..this is really helpful
Gnaniyar Zubair
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.
Avrom
i am facing one issue with above code. first string is displayed twicelike:String doctors1 = "VijayanVijayan, Zubair, Raja"
Gnaniyar Zubair
What is the string you use in parameter ?
Colin Hebert
A: 

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.

Albert
can u please elaborate how to use StringUtil for this?
Gnaniyar Zubair
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[] {','});
Albert
+3  A: 

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.

polygenelubricants
+1  A: 
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());
Ace.Yin