I have two questions

1. How do I use a Replace() function in JSP
String strName = "My name is";
String strName2 = replace(strName, "is", "is Michael");

2. How do I make a string to start with a capital letter

I want david to be David

Thank you.

Recommended Answers

All 3 Replies

replace() method is for switching characters or character seqeuances. However your example shows switching of strings/regular expresions so there is no point in using it. In this case you should use replaceAll(String regex, String replacement) .You have to be carefull with it, as it will be replace any matching expresion with your replacement string.

String strName = "My name is";
String expression = "is";
String replacement = "is Michael";
String strName2 = strName.replace(expression, replacement);

For second question you have to locate first the word of which you wish to change first character and can do something like this

String str = "david";
String str2 = 	str.replaceFirst( Character.toString(str.charAt(0)), Character.toString(str.charAt(0)).toUpperCase() );

Note please, that String str was in this case declared, where in your case you will have to locate it somehow!

Problem is that michael did not mentioned any DB usage and the recomended answer by you is only for Oracle/PLSQL.Also this function change all words to start with capital letter where from michael post I got impression that he want to do it only on certain words.
However you had a valid sugestion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.