How to print prefix and suffix without giving a String as an argument between them


Joined
May 9, 2022
Messages
1
Reaction score
0
I have a problem! I need to print symbols instead of the given chars! Example: Cake -> !)*& (Something like that). The problem is that I print them with System.out.println and it is fine, but there is a condition that I have to put another symbols as prefix and suffix, and I can't do that, because I print the symbol with switch(case), because I cannot give the variable a new value ( the symbol one). I've tried to make the new word from symbols with for-loop ( newWord += word.charAt), but doesn't work. Then I've seen the StringJoiner, but I want to print the prefix and suffix, without giving it argument, because I can't create the new word and I print the symbols if the char from the String is in the case. How to add the new symbols as first and last ? The stringJoiner wants argument if I use .add . My idea is to print the prefix and suffix, without giving a string. Please Help!
 
Ad

Advertisements

Joined
May 11, 2022
Messages
58
Reaction score
5
i'm having a hard time parsing your question.
a problem like this may be more suited to python, as it has several string modification functions
here's what i understand:
there is a condition that I have to put another symbols as prefix and suffix
so you're trying to add more characters to the beginning and end.
I've tried to make the new word from symbols with for-loop ( newWord += word.charAt)
so, are you trying to also change the symbols to other characters?
but I want to print the prefix and suffix, without giving it argument, because I can't create the new word and I print the symbols if the char from the String is in the case

i'm not sure what you're asking here. you just want to print the prefix and suffix, without the word you're adding them to?
that's fairly straight forward. just save them to a variable, and then print that variable
 
Ad

Advertisements

Joined
Mar 28, 2022
Messages
71
Reaction score
8
How to add the new symbols as first and last ?
Java:
System.out.println(
    String.format("%s with a middle %s and an end %s", prefix, middle, suffix)
);
Or
Java:
System.out.println( getPrefix() + middle + getSuffix() );
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top