Get Class name without package from StackTraceElement - java

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.

Related

How can i get part of the string from the array ? Index of?

Not sure how it's working in java.
In c# i used to use index of and substring.
private String[] ipaddresses = new String[]{
"http://10.0.0.4:8098/?cmd=nothing",
"http://10.0.0.3:8098/?cmd=nothing"};
private String iptouse = "";
I want to assign to iptouse the first index of the array ipaddresses but only part of the index:
iptouse = "http://10.0.0.4:8098/?cmd=" + "start";
It's only example what i want is to extract from the sting in index 0 only this part: http://10.0.0.4:8098/?cmd=
You can process it with String class or with Apache Commons StringUtils (they have a handful of cool methods for text parsing).
However, the propper way would be to use Java URI class.
There you have a very handy methods for extracting the host and the query part etc. To understand it better read about the structure of URI here
In your case it would be something like:
URI u = new URI("http://10.0.0.4:8098/?cmd=nothing");
String upToQuery = u.getScheme()+u.getAuthority()+u.getPath();
It's the same in Java use substring() and indexOf:
String iptouse = ipaddresses[0].substring(0,ipaddresses[0].indexOf('=')+1)+"start";
String symbol="=";
String iptouse =ipaddresses[0].split(symbol)[0]+symbol+"start";
Or
replaceFirst("nothing", "start");

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

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);

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.

Long to String in java

There is a code :
Long val = 10L
If I want to take its value as a String which approach is correct?
val.toString() or (String)val?
val.toString() would work.
If you are not sure if val can be null, you can also do String.valueOf(val)
You'd do it like this with the String class:
String s = String.valueOf(val);
If your Long might be null and you don't want to get a 4-letter "null" string, you might use Objects.toString, like: String s = Objects.toString(val, null);
You reverse it using Long val = Long.parseLong(String); but in this direction you need to catch NumberFormatException
You can also do:
Long.toString(val);
A typecast does not work since String is not a primitive datatype.

Howto convert the value of a hashmap to String i Java

I have a HashMap. I am trying to retrieve the value and print it using the key from the user-code.
The code is:
lib.addbook(book2.getISBN(), book2);
Book ret = lib.getbook("978-81-291-1979-7");
System.out.println(ret);
Current Output:
O/P: LibraryPackage.Book#527c6768
I want the output to be a string and to display the actual value not the address of the book.
You have to implement (and override) the toString() method in your Book class, and specify what you want the output to be. E.g.:
#Override
String toString()
{
return this.author+": " + this.title;
}
commons-lang has a great utility for this if you don't want to override the .toString() method, or need to represent it differently in different situations:
Here's a call to build a string based on reflection:
String str = ToStringBuilder.reflectionToString(object);
In fact, this is a great way to implement the .toString() method itself. Another alternative use of this class would be a field by field creation of the string:
String str = new ToStringBuilder(object)
.append("field1", field1)
.append("field2", field2)
.toString();

Categories

Resources