Having issues with replaceAll() [duplicate] - java

This question already has answers here:
String replace method is not replacing characters
(5 answers)
Closed 7 years ago.
Maybe I am using replaceAll incorrectly, but I am unable to find why it acts this way. I want to simply remove a $ sign from a string and then output the string.
public class Example{
public static void main(String[] args){
String s = "$50";
s.replaceAll("\\D+", "");
System.out.println(s);
}
}
However, this still outputs the $ symbol with the string. Does anyone know why this is happening?

You need to assign the return value of replaceAll to a variable:
s = s.replaceAll("\\D+", "");
because a String object is immutable.

Related

String to String array conversion with global variables [duplicate]

This question already has answers here:
How to Replace dot (.) in a string in Java
(4 answers)
Closed 2 years ago.
I have following problem with java.This method in Class should return String as is.
private String getAsString(Resource res) {
return "We wish you good luck in this exam!\nWe hope you are well pre-\npared.";
}
Then in Constructor this String shlud be converted into array of words
private int index;
private String string_arr[];
public TextFileIterator(Resource res) {
this.index=0;
if(res==null){
throw new NullPointerException();
}
String text=this.getAsString(res);
//text=text.replaceAll("-\n(?=[a-z])", "");
text=text.replaceAll("\\n", "");
text=text.replaceAll("!", " ");
text=text.replaceAll("-", "");
text=text.replaceAll(".", "");
this.string_arr=text.split(" ");
}
Problem is that at the end I get array which is null... what is the problem. I attach the debugger screenshots.
Please could explain me why does it happen?
The culprit is line no 17-
text=text.replaceAll(".", "");
The above line is replacing all of the content with "", because in regex world "." means any character.
Try this instead-
text=text.replaceAll("\\.", "");

How do I correctly get the value of 'ⅻ' (and other similar non-alphabetical characters) and store it in a string? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
Given a character that is not a standard alphabet character, such as 'ⅻ', I'm having problems converting it to a string and retaining it's value. For example If I have:
String myStr = "ⅻⅾ℡ℬ";
Character myChar = myStr.charAt(0);
Then System.out.println('ⅻ' == myChar); returns true, whereas System.out.println("ⅻ" == Character.toString(myChar)); returns false.
Thus my question effectively is how do I correctly get the value of 'ⅻ' and store it in a string?
Both of these conditions return true:
public class Test {
public static void main(String args[]) {
String myStr = "ⅻⅾ℡ℬ";
Character myChar = myStr.charAt(0);
System.out.println('ⅻ' == myChar);
System.out.println("ⅻ".equals(Character.toString(myChar)));
}
}

java strings inmutable but the code doesn't shows that [duplicate]

This question already has answers here:
Immutability of Strings in Java
(26 answers)
String is immutable. What exactly is the meaning? [duplicate]
(19 answers)
Closed 4 years ago.
I was learning string concepts, so wrote a code,expected a different output but got something very unexpected.
class stringmute
{
public static void main(String[] args)
{
String s1="Hello "; //string one.
System.out.println("Str1:"+s1);
String s2= s1+"world"; //New String.
System.out.println("Str2="+s2);
s1=s1+"World!!"; //This should produce only Hello right?
System.out.println("Str1 modified:"+s1);
}
}
when I execute the above code i get the output as:
Str1:Hello
Str2=Hello world
Str1 modified:Hello World!!
if i've done something wrong please let me know.
Since strings are immutable, which implies we should get the output of the "Str1 Modified" as "HELLO" instead of "HELLO WORLD!!".
When you assign s1 as :
s1=s1+"World!!";
New String created in jvm string pool and assigned to s1.
So it's value became "Hello World!!"

Java, how to check if a string contains a digit? [duplicate]

This question already has answers here:
Check and extract a number from a String in Java
(16 answers)
Closed 5 years ago.
how do I check if a string contains a digit?
public static void main(String[] args)
{
s1 = "Hello32"; //should be true`enter code here`
s2 = "He2llo"; //should be true
s3 = "Hello"; //should be false
}
With a regex you could search at least a digit among any (zero or more) characters:
boolean hasDigit = s1.matches(".*\\d+.*");
Check this it might help you
String regex = "\\d+";
System.out.println("abc45hdg".matches(regex));
In java
public boolean containsNumber(String string)
{
return string.matches(".*\\d+.*");
}

How do I use substring method in ruby? [duplicate]

This question already has answers here:
How to get a substring of text?
(5 answers)
Closed 8 years ago.
I made a program in java like this:
class substr{
public static void main (String[]args){
int n=1;
String s1="";
String s2=" ";
while (n<=5){
System.out.println(s2.substring(n,5)+n+s1+n);
s1=s1+" ";
s2=s2+" ";
n++;
}
}
}
I can't make use of substring in ruby, so I wanted to know if there is some way. I read about regex, but you don't use it in a program like this.
You can use array notation to substring an string. In your case, something like s2[n,5].

Categories