String.replace() isn't working like I expect - java

I am not understanding how to use the String.replace() method. Here is the code:
CharSequence oldNumber = "0";
CharSequence newNumber = "1";
String example = "folderName_0";
System.out.println("example = " + example);
example.replace(oldNumber, newNumber);
System.out.println("example.replace(oldNumber, newNumber);");
System.out.println("example = " + example);
And it's outputting:
example = folderName_0
example.replace(oldNumber, newNumber);
example = folderName_0 // <=== How do I make this folderName_1???

The replace method isn't changing the contents of your string; Strings are immutable. It's returning a new string that contains the changed contents, but you've ignored the returned value. Change
example.replace(oldNumber, newNumber);
with
example = example.replace(oldNumber, newNumber);

Strings are immutable. You need to re-assign the returned value of replace to the variable:
example = example.replace(oldNumber, newNumber);

String is a immutable object, when you are trying to change your string with the help of this code - example.replace(oldNumber,newNumber); it changed your string but it will be a new string and you are not holding that new string into any variable. Either you can hold this new string into a new variable, if you want to use your old string value later in your code like -
String changedValue = example.replace(oldNumber,newNumber);
or you can store in the existing string if you are not going to use your old string value later like -
example = example.replace(oldNumber,newNumber);

Related

Get Class name without package from StackTraceElement

I have this ste.getClassName() which return a String like this pack.age.Foo.
ste is StackTraceElement.
How I could get only Foo? Or the only way is to do a method which extract Foo from that string?
There isn't a built in method for that. You could break up the string like #Naya and #Daniel Perez suggested, or let Class to the heavy lifting for you:
String simpleName = Class.forName(ste.getClassName()).getSimpleName();
String fullClassName = stackTraceElement.getClassName();
String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
String[] parts = ste.getClassName.split(".");
parts[2] will be the Foo value.
.split allows you to choose a value for which the string will be divided into an array depending on the position of the divider.

Strange type of password in mysql workbench

I have a problem with password which I get when some password is inputed in JPasswordField.
kasirlozinka = new JPasswordField("lozinka");
final String lozinka = new String(kasirlozinka.getPassword().toString());
And when I write a password like "lozinka" in mysql workbench I get something like "[C#3f528528", so my questions are, how to fix this and get user input in string, and what type of password is this, how to decrypt it?
The call to JPasswordField.getPassword() returns a char[] (and you don't want to call toString() on that, because array doesn't override Object.toString). You can use String.valueOf(char[]) to make that a String. Something like,
char[] pass = kasirlozinka.getPassword();
String pw = String.valueOf(pass);
"[C#3f528528" is a String representation of a char[]. getPassword() returns an array of char. Just remove toString:
final String lozinka = new String(kasirlozinka.getPassword());
JPasswordField.getPassword() returns a char[]. Calling toString() on an array will yield what you see.
To convert the password to a string, remove the toString():
final String lozinka = new String(kasirlozinka.getPassword());
This will call the new String(char[]) constructor.
The toString() should be removed.
kasirlozinka.getPassword() returns char[] which can be passed into a String constructor :
kasirlozinka = new JPasswordField("lozinka");
//final String lozinka = new String(kasirlozinka.getPassword().toString());
final String lozinka = new String(kasirlozinka.getPassword());

replace String with value of properties file in java

I have a code to replace stream of string. I need to search a specific string that is defined in the key of properties file
String result="";
int i=0;
while (i<listToken.size()){
result = listToken.get(i);
while (enuKey.hasMoreElements()) {
String key = (String)enuKey.nextElement();
// String value = propertiesSlang.getProperty(key);
if (listToken.get(i).equals(key)){
String value = propertiesSlang.getProperty(key);
listToken.get(i).replace(listToken.get(i), value);
System.out.print("detected");
}
}
i++;
}
But it doesn't replace word. How I can replace words using properties.
It's because you forgot to assign the result, using the method set():
listToken.set(i, propertiesSlang.getProperty(key)));
assuming listToken implements AbstractList
Why complicate things with replace(). As far as I understand your code you can simply do -
String value = propertiesSlang.getProperty(key);
listToken.set(i, value);
I see you have modified your code again to
listToken.get(i).replace(listToken.get(i), value);
Just so that you know String class is immutable. So operations like replace() or substring() will give you a new String and not modify the original one. Get the new String and set it in your list listToken.

i have four string values from method. how to combine strings?

so i will get the value of these string from a method, and i need combine them as a file address, but i can't combine the string like i did on FILE_PATH_STRING. I tried to use concat method, but it doesn't work too. FILE_PATH_STRING will always equal to the first string ,which is WORLD_PATH in this case
public static final String WORLD_PATH = "The World/";
public static String CONTINENTS_NAME="";
public static String COUNTRY_NAME="";
public static String FILE_TYPE="";
public static String FILE_PATH_STRING = WORLD_PATH + CONTINENTS_NAME + COUNTRY_NAME + FILE_TYPE;
public static File FILE_PATH = new File(FILE_PATH_STRING);
it should work like, when i click on a map, method will return the name of region to me,and i wills store them in those static string. I tried debug. All of the strings have a value and they correct. but FILE_PATH_STRING only equal to the first string i put in there.
after i run the program,
CONTINENTS_NAME = Asia
COUNTRY_NAME should equal to CONTINENTS_NAME because that's how i set up my file address
FILE_PATH = .png this is method for load map
I am curious, how do you "have four string values from a method" - can you enlighten us on that? Because if it is anything like this:
getStrings(WORLD_PATH, CONTINENTS_NAME, COUNTRY_NAME, FILE_TYPE);
Then that's the problem - the function getStrings() can't modify those strings passed in.
I am only making wild guesses of course, since you haven't given enough information.
Can you do something like:
String path = getWorldPath() + getContinent() + getCountry() + getFileType();
That is, implement four separate methods to get the four separate parts of the path?
Otherwise you will have to define a type which can return all four strings at the same time, or return them in a container like an array:
void test()
{
String[] pathParts = getStrings();
FILE_PATH_STRING = pathParts[0] + pathParts[1] + pathParts[2] + pathParts[3];
}
String[] getStrings()
{
String[] ret = new String[4];
ret[0] = "The world";
ret[1] = "South America";
ret[2] = "Chile";
ret[3] = ".txt";
return ret;
}
Of course, if all you are going to do is concatenate the strings, you could do that in the getStrings() function.

Change an array to a string without creating a string

I wish to accomplish:
String []beef = new String[3];
beef[0] = "Water";
beef[1] = "Chicken";
beef[2] = "Paper";
String empo = Arrays.toString(beef);
if (empo.isEmpty()){
empo = "Nothing";
System.out.println(empo);
}else{
System.out.println(empo);
}
without having to create the string.
Something like:
String []beef = new String[3];
beef[0] = "Water";
beef[1] = "Chicken";
beef[2] = "Paper";
Arrays.toString(beef); //change beef to just a plain string
if(beef.isEmpty()||beef==""){
no = "Nothing";
System.out.println(beef);
How would one go about doing this?
You can't.
Java is a strongly and statically typed language. That means you have to tell it what type a thing will be when you declare it (strong typing), and you can't ever change it's type after that (static typing).
You will just have to create a new String.
You can create substrings with the same backing memory as the original string, but you can't create a string with the same backing memory as an array of strings. They're not stored in the same order so it's impossible to view the same memory both ways.

Categories

Resources