Hyphen To Beginning

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 3

Move hyphen to beginning

Move Hyphen to Beginning: Moving the hyphen to the beginning


refers to the task of repositioning a hyphen in a given string so
thaat it appears at the beginning. For example, transforming
“face-prep" into "-faceprep".
public class MoveHyphenToBeginning {
public static void main(String[] args) {
String originalString = "face-prep";
String transformedString = moveHyphenToBeginning(originalString);
System.out.println(transformedString);
}

public static String moveHyphenToBeginning(String string) {


if (string.contains("-")) {
int hyphenIndex = string.indexOf("-");
return "-" + string.substring(0, hyphenIndex) + string.substring(hyphenIndex + 1);
} else {
return string;
}
}
}
THANK YOU

You might also like