0

Suppose I am having two Strings as follows :

String name = "EXAMPLE_MODEL_1";
String actionName = "ListModels";

I want resulting string as Follows :

String result = "ExampleModel1ListModels";

I tried the Follwoing code :

String result = name.toLowerCase().replaceAll("_", "");
result = result.concat(actioName);

And I am getting the result value as "examplemodel1ListModels". But the exepected one is "ExampleModel1ListModels".

6
  • I dont understand ur Q,ur getting what u expected ,please correct typos if u have any Commented Jan 25, 2012 at 4:50
  • I could be wrong, but if I understand this correctly toLowerCase should convert result to lower case. Wouldn't that explain why you're not getting the original capitalized results? Commented Jan 25, 2012 at 4:50
  • it is edited by @lucifer there was a typo Example was Examole Commented Jan 25, 2012 at 4:52
  • because that was typing mistake in her question which none of you marked. Commented Jan 25, 2012 at 4:54
  • ppl r tempted to answer Commented Jan 25, 2012 at 5:05

7 Answers 7

2

The name string needs to have the underscores replaced -- you've done that. Before you do that, you need to convert it to title case.

After that, simply concatenate the two strings.

Sign up to request clarification or add additional context in comments.

Comments

1

Combine your partial solution, with the function described here:

What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?

Comments

1

You are using toLowerCase() method so you are getting result like that. Don't use this function.

2 Comments

Then which function to be used ?
@Beginner you need to find some function to convert your String to Title Case rather than Lower Case.
1

Use Apache's WordUtil.Capitalize Method.

Comments

0

Use Guava's CaseFormat

String result = LOWER_UNDERSCORE.to(UPPER_CAMEL, "EXAMPLE_MODEL_1") + "ListModels";

Comments

0

Apache commons-lang has utility classes that can help you. Below is my idea

  1. convert name to small case
  2. Use String capitalizeFully(String str, char[] delimiters) with delimiter as _
  3. Remove spaces out of result from step 1
  4. concatenate both

Comments

0

Try to use the following code (I'm editing and paste the full code)

import java.io.IOException;
public class StringTest{

    public static void main(String[] arg) throws IOException{
        String name = "EXAMPLE_MODEL_1"; String actionName = "ListModels";
        String result = toProperCase(name.toLowerCase().replaceAll("_", " "))+actionName;
        result= result.replaceAll(" ","");
        System.out.println(result);

    }
    public static String toProperCase(String theString) throws java.io.IOException{
        java.io.StringReader in = new java.io.StringReader(theString.toLowerCase());
         boolean precededBySpace = true;
         StringBuffer properCase = new StringBuffer();    
             while(true) {      
            int i = in.read();
              if (i == -1)  break;      
                char c = (char)i;
                if (c == ' ' || c == '"' || c == '(' || c == '.' || c == '/' || c == '\\' || c == ',') {
                  properCase.append(c);
                  precededBySpace = true;
               } else {
                  if (precededBySpace) { 
                 properCase.append(Character.toUpperCase(c));
               } else { 
                     properCase.append(c); 
               }
               precededBySpace = false;
            }
            }

        return properCase.toString();    

    }
}

Comments

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.