Tag Archives: string

String equality vs equality of location

Questions: String s1 = “BloodParrot is the man”; String s2 = “BloodParrot is the man”; String s3 = new String(“BloodParrot is the man”); System.out.println(s1.equals(s2)); System.out.println(s1 == s2); System.out.println(s1 == s3); System.out.println(s1.equals(s3)); // output true true false true Why don’t all the strings have the same location in memory if all three have the same contents?… Read More »

sorting 2D array of String in java

Questions: I know that this question might have been asked before, but I was not able to find a fit answer. So say I have this array: String [][] theArray = {{“james”, “30.0”},{“joyce”, “35.0”},{“frank”, “3.0”}, {“zach”, “34.0”}} Is there a way to descendingly sort this array by the second element of each sub-element. So I… Read More »

Hints for java.lang.String.replace problem? [duplicate]

Questions: This question already has an answer here: String not replacing characters 4 answers Answers: You need to assign the new value back to the variable. double myDouble = myObject.getDoubleMethod(); // returns 38.1882352941176 System.out.println(myDouble); String myDoubleString = “” + myDouble; System.out.println(myDoubleString); myDoubleString = myDoubleString.replace(“.”, “,”); System.out.println(myDoubleString); myDoubleString = myDoubleString.replace(‘.’, ‘,’); System.out.println(myDoubleString); Questions: Answers: The original… Read More »

Java: How to split a string by a number of characters?

Questions: I tried to search online to solve this question but I didn’t found anything. I wrote the following abstract code to explain what I’m asking: String text = “how are you?”; String[] textArray= text.splitByNumber(4); //this method is what I’m asking textArray[0]; //it contains “how ” textArray[1]; //it contains “are ” textArray[2]; //it contains “you?”… Read More »

Convert String from ASCII to EBCDIC in Java?

Questions: I need to write a ‘simple’ util to convert from ASCII to EBCDIC? The Ascii is coming from Java, Web and going to an AS400. I’ve had a google around, can’t seem to find a easy solution (maybe coz there isn’t one 🙁 ). I was hoping for an opensource util or paid for… Read More »

How to convert String to Date without knowing the format?

Questions: I have a problem. I am trying to convert some strings to date, and I don’t know the format the date is arriving. It might come as yyyy.mm.dd hh:mm:ss or MM.dd.yy hh:mm:ss and so on. How can I convert these strings to Date? I tried this: DateFormat formatter = new SimpleDateFormat(“dd-MM-yyyy hh:mm:ss”); Date d… Read More »

Count words in a string method?

Questions: I was wondering how I would write a method to count the number of words in a java string only by using string methods like charAt, length, or substring. Loops and if statements are okay! I really appreciate any help I can get! Thanks! Answers: public static int countWords(String s){ int wordCount = 0;… Read More »