Reputation: 103
In an exercise to make, I must to change the character after the dash in uppercase.
For example -> jean-louis becomes Jean-Louis
I know to change in uppercase the first caractere but after the dash I don't understand.
String name= null; // enter the name
String letterSlice = null; // slice the first letter
String letterSliceUpper = null; // first letter in uppercase
String remainingLetters = null; // remaining letters
int dashIndex = 0; // dash
System.out.print("Enter your name please : ");
name = input.next();
letterSlice = name.substring(0,1);
letterSliceUpper = letterSlice.toUpperCase();
remainingLetters = name.substring(1);
dashIndex = name.indexOf("-");
if(remainingLetters.indexOf("-") != -1 ){
}
System.out.print(letterSliceUpper.concat(remainingLetters));
Thank you for your help.
Upvotes: 1
Views: 112
Reputation: 1435
here is another solution using RegEx
String str = "jean-louis-patrice";
Pattern pattern = Pattern.compile("(^.|-.)");
Matcher matcher = pattern.matcher(str);
StringBuffer result = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(result, matcher.group().toUpperCase());
}
matcher.appendTail(result);
System.out.println(result.toString());
Upvotes: 2
Reputation: 444
public class MyClass
{
public static void main(String args[]) {
String s = "word-word";
String[] list = s.split("-");
String res = "";
for(int i = 0; i < list.length; i++)
{
String _temp = list[i].substring(0, 1).toUpperCase();
_temp += list[i].substring(1);
res += _temp;
if( i != list.length -1 )
{
res += "-";
}
}
System.out.println(res);
}
}
Upvotes: 0
Reputation: 37604
You need to keep the index of -
in order to replace the following character e.g.
int indexOfDash = name.indexOf('-');
Besides that I would convert the String into a Char array. It's way easier to manipulate. Something like
char[] charArray = name.toCharArray();
charArray[0] = Character.toUpperCase(charArray[0]);
if (indexOfDash != -1) {
charArray[indexOfDash + 1] = Character.toUpperCase(charArray[indexOfDash + 1]);
}
String newName = String.valueOf(charArray);
System.out.println(newName);
Upvotes: 0
Reputation: 2119
Use StringBuilder instead of creating a lot of String(Memory efficient). Try below:
System.out.print("Enter your name please : ");
name = input.next();
StringBuilder result = new StringBuilder(name.length());
String words[] = name.split("-");
for (int i = 0; i < words.length; i++){
result.append(Character.toUpperCase(words[i].charAt(0))).append(words[i].substring(1)).append("-");
}
System.out.println(result);
Upvotes: 0
Reputation: 201467
I would start by constructing a StringBuilder
(a mutable collection of characters), capitalize the first letter and then search for -
character(s). Capitalizing every character following a -
. Like,
String name = "jean-louis-patrice";
StringBuilder sb = new StringBuilder(name);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
int p = 0;
while ((p = sb.indexOf("-", p + 1)) != -1) {
if (p + 1 < sb.length()) {
sb.setCharAt(p + 1, Character.toUpperCase(sb.charAt(p + 1)));
}
}
System.out.println(sb);
Outputs
Jean-Louis-Patrice
Upvotes: 3