Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to reverse the characters in a string separated by a delimiter I provide.

Input: string: "Abc.134dsq" , delimiter: "."

Desired Output: cbA.qsd431

My attempt:

String fileContent = "Abc.134dsq";
String delimiter = ".";
fileContent = fileContent.replace(delimiter, "-");
String[] splitWords = fileContent.split("-");
StringBuilder stringBuilder = new StringBuilder();
for (String word : splitWords) {
    StringBuilder output = new StringBuilder(word).reverse();
    stringBuilder.append(output);
}

System.out.println(stringBuilder.toString());
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
158 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this:

System.out.println(Arrays
    .stream("Abc.134dsq".split("\.", -1))
    .map(StringBuilder::new)
    .map(StringBuilder::reverse)
    .collect(Collectors.joining(".")));

See live demo.

This handles the “preserving the trailing dot” scenarios mentioned in the comments. Live demo shows this aspect too.

Enough time has passed that your homework deadline has passed, so I thought I’d show you this one-liner.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...