开发者

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 Lett开发者_如何学编程er should display in capital. I need output like this:

String doctors1 = "Vijayan, Zubair, Raja"

How to do that?


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();
}


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.


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.


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());


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜