Java replace characters with uppercase around (before and after) specific character

后端 未结 2 807
慢半拍i
慢半拍i 2021-01-06 18:05

I have this kind of input

word w\'ord wo\'rd

I need to convert to uppercase both characters at the starts of the word and right after the <

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-06 18:14

    Not as elegant as @Wiktor Stribizew post above but an attempt to do without regex:

    public class HelloWorld{
    
     public static void main(String []args){
        String s ="word w'ord wo'r'd";
        System.out.println(upperCase(s,'\''));
     }
     private static int x = 1;
     private static String upperCase(String originalString, char delimeter)
     {
         if(originalString.length()==1)
         {
             return originalString;
         }
         int indexOfDelimeter = originalString.indexOf(delimeter);
         StringBuilder result = new StringBuilder();
         if(indexOfDelimeter<0)
         {
             return originalString;
         }
         String newBaseString = originalString.substring(indexOfDelimeter+2);
         if(indexOfDelimeter==0)
         {
             result.append(delimeter).append(Character.toUpperCase(originalString.charAt(indexOfDelimeter+1))).append(newBaseString);
         }
         else
         {
             result.append(originalString.substring(0,indexOfDelimeter-1)).append(Character.toUpperCase(originalString.charAt(indexOfDelimeter-1))).append(delimeter).append(Character.toUpperCase(originalString.charAt(indexOfDelimeter+1)));
         }
         if(indexOfDelimeter

提交回复
热议问题