R
rcurts
What I'm trying to do:
Take any string and uppercase the first character of each word, leaving
all other characters lowercase
ex: "THIS IS A STRING" becomes "This Is A String"
Problem:
I can get the method I created to uppercase the first char of a word if it
is separated by a space, however when I try to do the same with a
paren in the string, the next character is not set to uppercase.
Code Example:
--------------------------------------------------------------------
public class paren {
public static String s = "STRING THAT (HAS PARENS) INSIDE OF IT.";
public paren() {
System.out.println("Before UCW: " + s);
System.out.println(" After UCW: " + this.ucwords(s));
}
public String ucwords(String str) {
char[] broken = str.toLowerCase().toCharArray();
StringBuffer buf = new StringBuffer();
boolean doUpperCase = true;
for (int i = 0; i < broken.length; i++) {
if (doUpperCase) {
buf.append(String.valueOf(broken).toUpperCase());
doUpperCase = false;
} else {
if (String.valueOf(broken).equals("("))
doUpperCase = true;
else if (broken == ' ')
doUpperCase = true;
buf.append(broken);
}
}
return buf.toString();
}
public static void main(String[] args) {
new paren();
}
}
--------------------------------------------------------------------
There must be a silly mistake here, but I can't find it.
Any ideas?
Cheers,
Curts
Take any string and uppercase the first character of each word, leaving
all other characters lowercase
ex: "THIS IS A STRING" becomes "This Is A String"
Problem:
I can get the method I created to uppercase the first char of a word if it
is separated by a space, however when I try to do the same with a
paren in the string, the next character is not set to uppercase.
Code Example:
--------------------------------------------------------------------
public class paren {
public static String s = "STRING THAT (HAS PARENS) INSIDE OF IT.";
public paren() {
System.out.println("Before UCW: " + s);
System.out.println(" After UCW: " + this.ucwords(s));
}
public String ucwords(String str) {
char[] broken = str.toLowerCase().toCharArray();
StringBuffer buf = new StringBuffer();
boolean doUpperCase = true;
for (int i = 0; i < broken.length; i++) {
if (doUpperCase) {
buf.append(String.valueOf(broken).toUpperCase());
doUpperCase = false;
} else {
if (String.valueOf(broken).equals("("))
doUpperCase = true;
else if (broken == ' ')
doUpperCase = true;
buf.append(broken);
}
}
return buf.toString();
}
public static void main(String[] args) {
new paren();
}
}
--------------------------------------------------------------------
There must be a silly mistake here, but I can't find it.
Any ideas?
Cheers,
Curts