954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Replace String

I have two questions

1. How do I use a Replace() function in JSP
e.g.
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

e.g.
I want david to be David

Thank you.

michael.ngobeni
Newbie Poster
20 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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!

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

For the second Q, this http://www.techonthenet.com/oracle/functions/initcap.php should do the job :S

Sulley's Boo
Posting Pro in Training
452 posts since Dec 2004
Reputation Points: 529
Solved Threads: 10
 

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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You