changing String value based upon String[] index

how to replace a character at an index in a string in java
string replace at index javascript
string replace by index python
java replace multiple characters in string
how to replace a character in a string in java without using replace method
replacing substring in a string
java string replace substring
convert char array to string

i know how to do this

public class AccountPicker {

    public static void main(String[] args) throws InterruptedException {
        String password = "";
        String[] accounts = {"a", "b", "c"};

        Random r = new Random();
        int assigned = r.nextInt(3);

        if (assigned == 2) {
            password = "password";
            System.out.println("account: " + accounts[assigned] + " " + "password: " + password);
        }

        if (assigned == 1) {
            password = "Password";
            System.out.println("account: " + accounts[assigned] + " " + "password: " + password);
        }

        if (assigned == 0) {
            password = "PASSWORD";
            System.out.println("account: " + accounts[assigned] + " " + "password: " + password);
        }
    }
}

basically what this is doing is choosing a account a, b , or c and changing password based upon which index was picked out of accounts

but what if i have 100 values in accounts?

making 100 if statements would take up too much code..

is there a more efficient way of doing this?

You can create an array of passwords corresponding to each account e.g.:

import java.util.Random;

public class AccountPicker {

    public static void main(String[] args) throws InterruptedException {

        String password = "";
        String[] accounts = { "a", "b", "c" };
        String[] passwords = { "password", "Password", "PASSWORD" };

        Random r = new Random();
        int assigned = r.nextInt(3);

        System.out.println("account: " + accounts[assigned] + ", password: " + passwords[assigned]);
    }
}

A sample run:

account: c, password: PASSWORD

Replace a character at a specific index in a String in Java , We use cookies to ensure you have the best browsing experience on our website . By using our site, you acknowledge that you have read and� There is no predefined method in String Class to replace a specific character in a String, as of now. However this can be achieved indirectly by constructing new String with the 2 different substrings: one from beginning till the specific index – 1, the new character at the specific index and the other from the index + 1 till the end.

This task is especially suited for a Map. So you can also do it like this.

    Map<String,String> accts = Map.of("a","password", "b", "Password", "c", "PASSWORD");
    System.out.println(accts.get("c"));

Prints

PASSWORD

To see them all you can do it a variety of ways.

accts.forEach(System.out::println);

or

accts.forEach(e->System.out.println(e.getKey() + " " e.getValue());

Instead of giving you a number, they just provide the acct(or username). But use whichever is best for you.

Replace character at specific index in a Java String, Strings are constant in Java i.e. their values cannot be changed after they are created. In this post, we will see how to replace a character at a specific index in a Java String. The value[] is used for character storage in String class. field = String.class. Notify of new replies to this comment - (on). Notify of new replies to this� The index() method finds the first occurrence of the specified value. The index() method raises an exception if the value is not found. The index() method is almost the same as the find() method, the only difference is that the find() method returns -1 if the value is not found. (See example below)

You are basically mapping a number to a String. You could create an array of Strings, either hard-coded as you have or preferrably obtained from somewhere else, and then select a random value in that array.

Replace a Character at a Specific Index in a String in Java, I just announced the new Learn Spring course, focused on the Here, the idea is to convert the String to char[] and then assign the new char at� Description. Python string method index() determines if string str occurs in string or in a substring of string if starting index beg and ending index end are given. This method is same as find(), but raises an exception if sub is not found.

Manipulating Characters in a String (The Java™ Tutorials , You can get the character at a particular index within a string by invoking the charAt() String[] split(String regex, int limit), Searches for a match as specified by the string The String class does have four methods for replacing found characters or Then substring uses the return value of lastIndexOf to extract the file name� Strings are constant in Java i.e. their values cannot be changed after they are created. In this post, we will see how to replace a character at a specific index in a Java String. String is an immutable class in java. That means we cannot make any change in the String object.

String (Java SE 11 & JDK 11 ), Index values refer to char code units, so a supplementary character uses two positions in a String . The String class provides StringConcatFactory depending on the JDK version. This method does not properly convert bytes into characters. String(byte[] String(byte[] bytes, int offset, int length, String charsetName). If that substring is not available in the string, the returned index would be -1. IndexOf(String substring, int startindex) This Java method returns the index of the first character in the substring passed as the first parameter, after the "startindex" index value.

Index, Slice, Split, and Manipulate JavaScript Strings, How To Index, Split, and Manipulate Strings in JavaScript. How To How To Convert Data Types in JavaScript To summarize, charAt() and slice() will help return string values based on index numbers, and indexOf() and� The solution is to create a new string with the given char replaced or removed. Replace There are multiple ways to achieve the replacement, e.g. using StringBuilder or String.ToCharArray();