• Post Reply Bookmark Topic Watch Topic
  • New Topic

Switching half a string to the front?  RSS feed

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have to write a program where I am encrypting a password. For one part of the program I have to switch half of the inputted password with the other half. So for example is my password was "Java" it would become "vaja" but I have no idea how to do this. Can someone please tell me how I would go about doing this? I think I need to create a method for it but I am not sure. Thanks in advanced.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:So for example is my password was "Java" it would become "vaja"
This logic would be for password having even length. Have you thought about password having odd length. Example what if somebody sets password "Javaa" how would you apart them? Also keep in mind password should be case sensitive means "Java" and "java" are not equal. Go to link provided by Knute Snortum. Too many ready made String functions are available, utilize them and update status if you get any problem in code.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the String into a StringBuilder(←link) because that gives much more options for changing things. Note many of its methods return StringBuilder so you can chain method calls like this:-
builder.delete(...).append(...);
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I was able to make a little program to split a string in half.

but how do I go about converting this into one string? Because if I just print one string after the other it would give the appearance that it did what it was supposed to do but in truth it actually wouldn't be. Plus there are more steps after this. So what should I do?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:. . . but how do I go about converting this into one string? . . .
What is wrong with the String catenation operator? firstHalf + secondHalf

You appear not to have told us the full problem. What is efficient for one stage may turn out to be inefficient when you add the subsequent stages.
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
John Sing wrote:. . . but how do I go about converting this into one string? . . .
What is wrong with the String catenation operator? firstHalf + secondHalf

You appear not to have told us the full problem. What is efficient for one stage may turn out to be inefficient when you add the subsequent stages.


Actually this would work fine. It was a mistake on my part when I said it wouldn't. Thanks for all the help.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!