String 1 « string « Java Data Type Q&A

1. What's the best way to build a string of delimited items in Java?

While working in a Java app, I recently was needing to assemble a comma-delimited list of values to pass to another web service without knowing how many elements there would be ...

2. How to perform string Diffs in Java?

I would need to perform Diffs between Java strings. I would like to be able to rebuild a string from the original string and diff versions. Does anyone has done this ...

3. Why does the string "¿" get translated to "¿" when calling .getBytes()

When writing the string "¿" out using

System.out.println(new String("¿".getBytes("UTF-8")));
¿ is written instead of just ¿. WHY? And how do we fix it?

4. String Operations in Java

I know this is a bit of a newbie question, but are there equivalents to C#'s string operations in Java? Specifically, I'm talking about String.Format and String.Join.

5. Java Strings: "String s = new String("silly");"

I'm a C++ guy learning Java. I'm reading Effective Java and something confused me. It says never to write code like this:

String s = new String("silly");
Because it creates unnecessary String objects. ...

6. How to increment a java String through all the possibilities?

I need to increment a String in java from "aaaaaaaa" to "aaaaaab" to "aaaaaac" up through the alphabet, then eventually to "aaaaaaba" to "aaaaaabb" etc. etc. Is there a trick for this? ...

7. Management of Java string resources

I'm working on a java SE 1.5+ swing application, in conjunction with others. I'm wondering what the best way of managing string resources is. I understand the principles behind resource bundles ...

8. String altering in Ant

Given a property:

<property name="classes" value="com.package.Class1,com.package.Class2" />
I'm trying to compile only the classes specified like:
<javac srcdir="${src.dir}" destdir="${build.dir}">
    <include name="${classes}" />
However the 'include' tag is specifying the file names to ...

9. String conversions

I have a method which takes String argument.In some cases i want to pass int value to that method.For invoking that method i want to convert int into String.For that i ...

10. Trouble handling input in Java

I'm trying to read input from the terminal. For this, I'm using a BufferedReader. Here's my code.

BufferedReader reader = new BufferedReader(new InputStreamReader(;
String[] args;
do {

11. Java alphabets in different languages

How can I determine if String contains only alphabets and I want to have little more than [a-zA-Z]+, so is there any way to determine alphabets by Locale?

12. Java string inside string to string

I have this string: "\"Blah \'Blah\' Blah\"". There is another string inside it. How do I convert that into: Blah 'Blah' Blah? (you see, unescaping the string.) This is because ...

13. Java multiline string

Coming from Perl, I sure am missing the "here-document" means of creating a multi-line string in source code:

$string = <<"EOF"  # create a three line string
In Java I have to ...

14. Am I correctly interning my Strings?

I want to make sure I don't pummel the permgen space, so I'm carefully interning my strings. Are these two statements equivalent ?

String s1 = ( "hello" + "world" ).intern(); 

String s2 ...

15. how to process string in java

I want to make strings like "a b c" to "prefix_a prefix_b prefix_c" how to do that in java?

16. Is there a semi-automated way to perform string extraction for i18n?

We have a Java project which contains a large number of English-language strings for user prompts, error messages and so forth. We want to extract all the translatable strings into a ...

17. text-area-text-to-be-split-with-conditions repeated

I have a text area wherein i have limited the user from entering more that 15 characters in one line as I want to get the free flow text separated into ...

18. How do you draw a string centered vertically in Java?

I know it's a simple concept but I'm struggling with the font metrics. Centering horizontally isn't too hard but vertically seems a bit difficult. I've tried using the FontMetrics getAscent, getLeading, getXXXX ...

19. Is java Runtime.exec(String[]) platform independent?

I had some code that ran commands through Runtime.getRuntime.exec(String), and it worked on Windows. When I moved the code to Linux, it broke, and the only way of fixing it was ...

20. deleting a string

I have an array of strings, and I want to delete a particular string from that array. How can I do that? My code is:

 private void nregexp(){

21. How to create an object from a string in Java (how to eval a string)?

I know eval is "evil", but I'm using it in a way that the user can't ever abuse it. Let's say I've got a string "new Integer(5)". I want to do ...

22. Java compiler handles inline Strings efficiently?


static final String memFriendly = "Efficiently stored String";
System.out.println("Efficiently stored String");
Will Java compiler treat both (1 and 2) of these in the same manner? FYI: By efficiently I am referring to runtime memory ...

23. Simple way to repeat a String in java

I'm looking for a simple commons method or operator that allows me to repeat some String n times. I know I could write this using a for loop, but I wish ...

24. Safe to call underlying java method on String in ColdFusion?

Adobe ColdFusion is built on Java. Almost all simple variables in CFML/CFSCRIPT are java.lang.String until the operation needs it to be of a certain type. I've always want to use startsWith() ...

25. Creating Strings from Bytes/Ints in Java

I'm wondering why the following code doesn't work:

String test = new String(new byte[] {92, 92, 92, 92, 92});
String compare = "\\\\\\\\\\";
if (test == compare) {
The output is:
Where is a data type ...

26. java basics about String

To what value is a variable of the String type automatically initialized?

27. Input string compressed as string

I want to compress/transform a string as new string. i.e.: input string:

output string after compress:
output string after decompress:
There are some compress or hash method for this transformation? I prefer some solution using Java or ...

28. Bad to use very large strings? (Java)

Are there any negatives to creating huge strings? For instance, if we're reading in text from a potentially huge text file:

while (scanner.hasNext()) {
  someString +=;
// do something cool ...

29. Optimizing if-else /switch-case with string options

What modification would bring to this piece of code? In the last lines, should I use more if-else structures, instead of "if-if-if"

if (action.equals("opt1"))
    if ...

30. Java Properties Object to String

I have a Java Properties object that I load from an in-memory String, that was previously loaded into memory from the actual .properties file like this:

this.propertyFilesCache.put(file, FileUtils.fileToString(propFile));
The util (fileToString) actually reads ...

31. Java string to bytearray back to string

I have a client and server java application that needs have encrypted text passing through each other. I am using an XOR encryption the encrypt the text that I want. The problem ...

32. Using an input string in a textbox

I am trying to have the user input a number, and then that number is used to populate a text field on a jform. However it keeps giving me errors. ...

33. Modify a string in Java bytecode (jar)

I want to modify a connection string that's hard-coded in a Java application (jar without source). I presume it's possible to decompile the jar, then change the source and recompile to ...

34. Question on String Operation

My understanding of java String got wrong when i see this code. I am not sure how this is happening. Can anyone explain why is so?.

public class NewClass {

35. Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program ...

36. Which is more efficient?

Just curious, which is more efficient? This:

String a = someString + "." + anotherString;
Or this:
String a = someString + '.' + anotherString;

37. Java parseFloat seems to be acting inconsistently

I am sure this has more to do with my understanding of the behaviour than any inconsistent action on the part of parseFloat, but bear with me... if I have input in ...

38. What should I do for separating a string?

For example I have a string "1234" that I want to break into two strings "12", "34". Is there any method for doing that? How?

39. String relate source code

Question 1. I want to print my from the

String s = " This is my house";
using core java? Question 2. I want to print house is my This from the
String s ...

40. Is there a "fastest way" to construct Strings in Java?

I normally create a String in Java the following way:

String foo = "123456";
However, My lecturer has insisted to me that forming a String using the format method, as so:
String foo = ...

41. Java String in Javascript function

I want to pass a Java string variable to Javascript function which I am calling in Java code. Somehow the value of string remains as null. Is there any way to ...

42. Java Strings and StringPool

public String makinStrings() {
   String m = "Fred47";
   String s = "Fred";
   s = s + "47";
   s = s.substring(0);
   return ...

43. Could I change the reference inside one method with this reference as argument in Java?

private static void changeString(String s) {
    s = new String("new string");

public static void main(String[] args) {
    String s = new String("old string");

44. Breaking up a String in Java

I have multiple strings that are in the following format:
12/18/2009 02:08:26 Admitted Doe, John (Card #111) at South Lobby [In]

From these string I need to get out the ...

45. Strings are objects in Java, so why don't we use 'new' to create them?

We normally create objects using the new keyword, like:

Object obj = new Object();
Strings are objects, yet we do not use new to create them:
String str = "Hello World";
Why is this? Can ...

46. Sorrounding Logger with an If clause to avoid redundant String construction

I got a recommendation to use this syntax when logging in java:

if (logger.isLoggable(Log.FINE))
    logger.fine("bla"+" bla"+" bla");
The reason for this is to avoid the redundant construction of the parameter ...

47. Returning String[] from a method

I want to return two values from a method stored in an array. How can I do it?
For example: The method needs to return "un" and "pwd".

48. In Java, how to get attribute given the string with its name?

I'm sorry for asking this sort of questions, but I really couldn't find the answer in Google. So say I have a class with private String myColor and I have a ...

49. How to create a String class replica?

I need to create a class with exactly the same methods as java.lang.String. What is the best way to do this in Java? I know that I can't extend String class as ...

50. Why is String final in Java?

From when I learned that the class java.lang.String is declared as final in Java, I was wondering why is that? I didn't find any answer back then, but this post:

51. Java unreadable strings

I have made a java socket listener which listens on port 80. And what is basically does is it gathers the data that it listens on port 80 and stores it ...

52. Why java has "String" type and not "string"?

Wrapper class are just fine and their purpose is also well understood. But why do we omit the primitive type ?

53. Garbage collection and Strings

I have some doubts regarding Strings Are they live on Heap or String pool? And if on Heap then they will be garbage collected, if they are not reachable by any live thread. And ...

54. Shorten a String

Is there a better way to shorten (Use fewer characters) a String in java besides converting the chars to int's and running them through base36? For example, say if I wanted to ...

55. Can I multiply strings in java to repeat sequences?

I have something like the following:

int i = 3;
String someNum = "123";
I'd like to append i "0"'s to the "someNum" string. Does it have some way I can multiply a ...

56. String isNullOrEmpty in Java?

This surely has been asked before, but Googling doesn't find it. Is there, in any of the standard java libraries (including apache/google/...), a static isNullOrEmpty() method for Strings?

57. Levenshtein distance on non-English strings

Will the Levenshtein distance algorithm work well for non-English language strings too? Update: Would this work automatically in a language like Java when comparing Asian characters?

58. java replaceLast()

Is there replaceLast() in Java?
I saw there is replaceFirst() EDIT: If there is not in the SDK, what would be a good implementation?

59. how to return two strings ans1, ans2

public String[] decode(String message)
     String ans1 = "hey"; 
     String ans2 = "hi";  
     return {ans1 , ans2}; ...

60. question with returning an accessor method

How would I get the accessor method to return the first letter of a name to be uppercase and the rest in lowercase no matter what was entered?

public class Name

61. how to divide the string in java

hi i have a sting like this "hello" -- i want to divde the characters into h,e,l,l,o how can you do this in java by stringtonizer... can you suggest me

62. Java loss of precision

I have a problem concerned with losing of precision my task is to print numbers as strings

int exponent = ...
int[] Mantissas = { 1, 2, 5 };
double dataStep = java.lang.Math.pow(10.0, exponent) ...

63. Front-popping a Java String

Having read the documentation of Java's String class, it doesn't appear to support popping from front(which does make sense since it's basically a char array). Is there an easy way to ...

64. Regarding Java Command Line Arguments

I have the below command line arguments set for the program. argument proc is mandatory argument chgval is optional and argument inputfile is optional. ./ -proc mode1 -chval ...

65. String division

Hi I want to print the string " my name is xxx" as "xxx is name my" with out using Special methods (like util package methods in java); thanks

66. Do resource bundles in Java support runtime string substitution?

Can you do the following with a Java ResourceBundle? In the properties file...

example.dynamicresource=You currently have {0} accounts.
At runtime...
int accountAcount = 3;
To give a result of "You currently have 3 accounts."

67. When I do ""+1 I get a String - Why

Please understand firstly that I fully understand that Java will return a String when I use ""+int. What I'm really not sure about is what exactly is happening down at the ...

68. Explain JAVA code

I need some help to explain the meaning from line 5 to line 9. Thanks

  1. String words = "Rain Rain go away";
  2. String mutation1, mutation2, mutation3, mutation4;
  3. mutation1 = words.toUpperCase();
  4. System.out.println ("** " + ...

69. fuzzy implementaion for capture specific strings

I am going to develop a web crawler using java to capture hotel room prices from hotel websites. In this case i want to capture room price with the room type ...

70. Lost String garbage collection

Lost String objects are memory wasters is it? How can garbage collection be invoked for the same?

71. How to get Locale from its String representation in Java?

Is there a neat way of getting a Locale instance from its "programmatic name" as returned by Locale's toString() method? An obvious and ugly solution would be parsing the ...

72. catDog string problem at

Could anyone check my solution? I want to return true if the string "cat" and "dog" appear the same number of times in the given string. There are various strings with different ...

73. Document to String?

I've been fiddling with this for over twenty minutes and my Google-foo is failing me. Let's say I have an XML Document created in Java (org.w3c.dom.Document):

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document ...

74. how to retrieve part of a string in java?

I am designing a chat applet. In this, a user will append his name to the beginning of the message that he sends to the other user. In the window of ...

75. Why the output is not same?

The output of the fist System.out.println() is not same as the second System.out.println()
What may be the reason?

public class swapex{
    public static int var1, var2;

    public ...

76. How do I avoid repetition in Java ResourceBundle strings?

We had a lot of strings which contained the same sub-string, from sentences about checking the log or how to contact support, to branding-like strings containing the company or product name. ...

77. Transposing and Untransposing a String in java

I have been working on two methods that will Transpose and Untranspose a String respectively. The solutions that I have come up with both work to the best of my knowledge. ...

78. how to add a hyphen in between a string

I have the following sting xxxxx, I want to add a hyphen like x-xxxx, how can I do so using Java?

79. How would I create an object that uses String type behaviour for creation?

I'd like to be able to create an object that is created like a String object, and when created verifies that the String value is an appropriate option. I.E. SpecificString can be ...

80. Java Anagram Solver

I can work out how to create anagrams of a string but I don't know how I can compare them to a dictionary of real words to check if the anagram ...

81. public String shorthand(String in)

I am stuck on this code. The code should use the class StringBuilder to build an output string by appending non-vowel characters from its argument ...

82. How to make a NON-LINE based diff between two strings in Java

I have to compare the text content of two xml elements, eg.

<p>During lunch he hears strangers whispering, catching the name "Potter" and "Harry", and someone saying You-Know-Who has gone at last. ...

83. Calling a method named "string" at runtime in Java and C

How can we call a method which name is string at runtime. Can anyone show me how to do that in Java and C. Thanks for replying.

84. The method replaceVariables(String) is undefined for the type

I get this message for as many times as I have used replaceVariables in my code. I have added the referenced libraries, but I don't know what else to do. Can ...

85. What classes do you use to make string templates?

What classes do you use to make string placeholders work?

 String template = "You have %1 tickets for %d",
 Brr object = new Brr(template, {new Integer(1), new Date()});

86. Java String object creation

Possible Duplicate:
What is the purpose of the expression “new String(…)” in Java?
Hi, 1) What is difference in thers two statements:
String s1 = "abc";
String s1 ...

87. Create Java method from a string?

Is it possible in Java, to declare a method with a string instead of an identifier? For example can I do something like the following:

class Car{

     new Method("getFoo", ...

88. new String() in java

Possible Duplicate:
What is the purpose of the expression “new String(…)” in Java?
I've found some old code that has new String("somestring") in it. Has there ...

89. How do I apply a sequence of insert/delete-character operations on a string?

I have a text like this:

My name is Bob and I live in America.
I have some reference to the characters of this string, for example:
from 3 to 7 chars, ...

90. string cutting challenge to get one dir level up efficently

what is the most efficient way to get one directory tree level up ? i am looking for most efficient use of string class method to getting e:\files\report\fruits from e:\files\report\fruits\apples

91. String vs. A new Data class

I overheard two of my colleagues arguing about whether or not to create a new data model class which only contains one string field and a setter and a getter for ...

92. Split/tokenize/scan a string being aware of quotation marks

Is there a default/easy way in Java for split strings, but taking care of quotation marks or other symbols? For example, given this text:

There's "a man" that live next door 'in my ...

93. avoid special symbols in string in java

i want to know how to get string only in my data using regex thanks in advance.

94. java macroresolver

Hello I am a newbiew to java. I am trying to write an macroresolver I have string which is represneted as

String s = '{object.attribute}'  --> result of the resolver should be ...

95. Automatically ellipsize a string in Java

Is there a method in Java to automatically ellipsize a string? Just in Java, not other libraries. Thanks.

96. How to send a dial string from the computer to asterisks to call a phone?

I've been trying to figure out how to send a dial string to the asterisks server which should then handle a call to the phone with that extension. I know there ...

97. ' " ' and " ' " in Java .. How to deal with them?

I'm writing this in JAVA :

This is has to ...

98. Java == for String objects ceased to work?

public class Comparison { public static void main(String[] args) { String s = ...

99. Has anyone encountered a universal Object to String formatter for Java?

Has anyone encountered a universal Object to String formatter for Java? I'm imagining something that would use reflection to discovery properties of an arbitrary object (probably just a JavaBean, but ...

100. Efficient in Java strings

This sentences are equals myString != null, myString.length() > 0 and ! myString.equals("") ? Wich is the most efficient? (Java 1.4)