I am trying to find all characters, which are not letters(upper/lowercase), numbers, and underscore, and remove it. However, the following code could not even compile in Java: If I use only “\W” rather than “W”, the above code turns out to be correct. So, what is the differences between W, \W, and when to use brackets like [^a-zA-Z0-9_] Answer However,
Tag: string
In Java, which is faster – String.contains(“some text”) or Regex that looks for same text?
As the title says, just looking for a string to match a client finishing sending data over a socket, so I might be looking for something like {“Message” : “END”} in a JSON string for example. A the most the strings will be a few hundred chars long. Answer They’re both fast enough to be over before you know it.
Coding Bat Recursion exercise (java)
How can I solve this problem recursively instead of using the replace method? I’m trying to get better a recursive methods. Given a string, compute recursively (no loops) a new string where all the lowercase ‘x’ chars have been changed to ‘y’ chars. I cheated and solved it this way and tried to look at the source code for Java
Why show java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String?
When copying String from any browser page, pasteData works properly. However when copying SpannedString from a message sent item editor(field), the application crashes and shows this error message: My code: where the ClipboardManager instance defined as clipBoard, below: All I’m trying to do is use pasteData in String format. How to get rid of this error? Any help is appreciated.
contains() method not working as expected
I am building a voice assistant for android, here this method retrieves contact names one by one and compares it to to SpeechToText input. I am successfully getting the contact names, but when I am comparing it with my input text, nothing is happening. Here is the code Here for example I sending “call karan” as input, while debugging the
Why String created using new operator creates string literal in string pool
My Question is what’s the use of creating string object in string pool as well as on Heap when we declare String as String a = new String(“abc”); What is the advantage ? And why not we create string in heap when we create string as String a = “abc”. Answer The java language was designed like that. Anything you
Ignoring upper case and lower case in Java
I want to know how to make whatever the user inputs to ignore case in my method: Answer You have to use the String method .toLowerCase() or .toUpperCase() on both the input and the string you are trying to match it with. Example:
Java – Why can’t I use charAt() to see if a char equals another?
I want to see if a character in my string equals a certain other char value but I do not know what the char in the string is so I have used this: But I get the error: But “g” == “h” seems to work and I know you can use ‘==’ with char types. Is there another way to
How to compare two string dates in Java?
I have two dates in String format like below – I want to make sure startDate should be less than endDate. startDate should not be greater than endDate. How can I compare these two dates and return boolean accordingly? Answer Convert them to an actual Date object, then call before. Recall that parse will throw a ParseException, so you should
String.valueOf(Integer) always return 1
I’m trying to parse String “2 2 2” and add every symbol ‘2’ in ArrayList. This is my code: I can’t understand why System.out.println(String.valueOf(myIntArray.get(rawSize)));always return 1 ? UPDDATE: I try read file, which contain next text: Here is my Main function: public static void main(String[] args){ My output: When i replace:a_char = line.charAt(charCounter); with a_char = ‘5’;, my output: Can’t