StringBuffer 2 « StringBuffer « Java Data Type Q&A





1. how to trim a StringBuffer??!!    coderanch.com

2. Process waits forever when using Stringbuffer with more than 2000 strings    coderanch.com

Hi, I am using ProcessBuilder to invoke an external command which will provide me the output. And I'm supplying input through String Buffer. My program works fine for 2934 strings within StringBuffer. However, the moment my stringbuffer size goes beyond 2934, the program hangs & waits forever. Below is my code. import java.io.*; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; class check ...

3. Need Help in to write query using StringBuffer    coderanch.com

Hi All, I have five textfields and a search button in my JSP page. I can fill any one of those five textfields or i can fill all five fields to search. i need to take all the parameters in my action class and validate which one is not null and add that particular value to my query string. i found ...

4. String and StringBuffer    coderanch.com

String is good for coding. StringBuilder is good for coding. That question is a bit like asking whether a bicycle is good for going there, or a boat. Both are good, but it depends where you are going. If you want to go across the river and there is no bridge, you need a bicycle. If you want to go up ...

5. How is StringBuffer Implemented    coderanch.com

6. JAVA StringBuffer line breaking    coderanch.com

By the way, I've already done half of the program: public class StringReader{ public static void main (String[] args){ String string = ""; String length = ""; int i = 0; boolean IntegerCheck = true; Scanner in = new Scanner(System.in); System.out.println("Insert your string:"); string = in.nextLine(); string = string.replaceAll("\\s+"," ").trim(); while(IntegerCheck){ System.out.println("Insert Integer:"); try { length = in.nextLine(); i = Integer.parseInt(length); ...

8. Diiference between String ,StringBuffer & StringBuilder    coderanch.com

String is immutable. The content of a String object never changes after it has been created. StringBuffer and StringBuilder represent mutable sequence of characters. StringBuffer and StringBuilder have the same function, but StringBuilder is newer and should be preferred. The difference between the two is that most of the methods of StringBuffer are synchronized, while the methods of StringBuilder are not. ...





10. String and StringBuffer    coderanch.com

11. Reasons for using StringBuffer or StringBuilder classes rather than String    coderanch.com

Hi there, I'm still not 100% sure about why I would choose to use StringBuffer or StringBuilder classes rather than Strings. At first I thought it was because all String objects are referenced from the String Constant Pool and so a lot of memory would be used up if you had lots of String manipulation to do. But after reading this ...

12. Doubt regarding StringBuffer    coderanch.com

public class StringBufferExample { public static void main(String [] args){ StringBuffer sb1 = new StringBuffer("a"); StringBuffer sb2 = new StringBuffer("b"); append(sb1,sb2); System.out.println(sb1 + "," + sb2); } public static void append(StringBuffer sb1, StringBuffer sb2){ sb1.append(sb2); sb1 = sb2; sb1.append(sb2); System.out.println("in append method");//insert this line will make it more clear System.out.println(sb1 + "," + sb2); } } sb1-----------a sb2-----------b after append(sb1,sb2) this ...

14. StringBuffer    coderanch.com

15. reverse position of String into a StringBuffer    coderanch.com

Hello, I wouldl like to reverse the position of 2 string into a string buffer. For example, if my string buffer contains: value1value2value3 I would get this at the end: value2valuer1value3 I tried with the replace method of String class, but it only erase a chain over another .... Thank you for your help! public static void main(String[] args) { String ...

16. what is the difference between String and StringBuffer?    coderanch.com

public class Test { public static void main(String[] args) throws Exception { String str1 = "Hello "; String str2 = "EveryBody"; str1 +=str2; } }When I compile that, then decompile that using JAD I get this: // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://www.kpdus.com/jad.html // Decompiler options: packimports(3) // Source File Name: Test.java public class ...





17. StringBuffer    coderanch.com

First of all, unless you need the synchronization use a StringBuilder instead of a StringBuffer. In at least 99.999% of the cases you really want each thread to have its own StringBuilder. If you would share the same StringBuilder, anything you add to it from one thread becomes visible in all the other threads as well. And that's usually not what ...

18. StringBuffer doubt    coderanch.com

I was reading about StringBuffer class and i came to know that the default capacity when using the constructor with no parameters is 16. i.e.StringBuffer s=new StringBuffer(); Capacity of s is 16 characters. But when we add the 17th character then the capacity increases by (2 * previous capacity + 2) i.e. (2 * 16 + 2)=34. Therefore after adding the ...

19. String and StringBuffer    coderanch.com

Hi Every one, I hava a reverse string program which i coded by using both String and StringBuffer. I am posting it .. import java.lang.StringBuffer; public class ExampleOne { public static String reverse(String s) { if(s.length()<=0) throw new EmptyStringFoundException("Empty String Provided"); else { //This part of code i am writing in two ways. below the code u check the code.. } ...

20. How free StringBuffer?    java-forums.org

21. How to do this with StringBuffer???    java-forums.org

22. StringBuffer .. and stringtokenizer    java-forums.org

23. How to use a StringBuffer?    java-forums.org

So... I was trying to use this: Is there any method available to insert a character at a specific position in a string? For example: st = "abde" <------ Original string st= "abCde" <------ Add "C" in position 2 You can use StringBuffer to do this: String st = new String("abcde"); st = StringBuffer(st).insert(2, "C").toString(); as an example for what I'm ...

24. StringBuffer    java-forums.org

25. StringBuffer StringBuilder String    forums.oracle.com

26. StringBuffer    forums.oracle.com

You are correct in your statement that StringBuffer is mutable. Thats why your stringbuffer b gets the content BA. What you are probably confused about is that the reference to the parameter y (and x as well for that matter) is passed by value. therefor you cannot modify the original reference inside the method. There are threads on this forum where ...

27. StringBuffer    forums.oracle.com

28. stringbuffer question - simple    forums.oracle.com

29. StringBuilder v/s StringBuffer    forums.oracle.com

Hi All, I want to know the difference between StringBuilder & StringBuffer classes. I know that StringBuffer is thread safe and StringBuilder is not, but any practical examples and code would be appreciated. Also, if we mark the methods written for StringBuilder objects as 'synchronized', would they become thread safe? please respond soon, its urgent. Thanks In Advance!

30. Can't print value of StringBuffer!!!    forums.oracle.com

@SunFred There is no output.Its a blank screen. @duffymo Sir i know u might be having lots of experience in java but its just beginning for me.No disrespect to you but I believe one day i ll be using those awful words for some of your codes.You should help me rather than discourage me by saying those 'awful' words.

31. stringBuffer object    forums.oracle.com

32. some confusion related to stringbuffer    forums.oracle.com

33. help w/ StringBuffer class    forums.oracle.com

34. StringBuffer related problem    forums.oracle.com

35. Confusion Between String and StringBuffer    forums.oracle.com

Thx for Reply Buddy But I 've some more confusion public class AQuestion { public void method(Object o) { System.out.println("Object Verion"); } public void method(String s) { System.out.println("String Version"); } public static void main(String args[]) { AQuestion question = new AQuestion(); question.method(null); } } The Output of the following is "String Version". Although we're calling a method with a null parameter ...

36. StringBuffer    forums.oracle.com

b is not the same as y. They are two different references that were originally references to the same object. When you change the object that y refers to, that does not mean that the object that b refers to also changes! It still is a reference to the same original object, the StringBuffer "B"

37. StringBuffer problem    forums.oracle.com

38. Problems with StringBuffer    forums.oracle.com

39. Palindrome using StringBuffer    forums.oracle.com

40. Difference between String and StringBuffer    forums.oracle.com

41. StringBuffer and String    forums.oracle.com

Hello I have list of string objects (very big list) and want to combine those strings together. I used two methods: ====== String myString=""; for (int i=0; i< myList.size() i++){ myString = myString + (String)myList.get(i); } return myString === String Buffer myStringBuffer= new StringBuffer(); for (int i=0; i< myList.size() i++){ myStringBuffer = myStringBuffer.append((String)myList.get(i)); } return myStringBuffer The second method run very ...

42. help regarding StringBuffer    forums.oracle.com

I have a StringBuffer.Using a while loop i am storing something of this sort in the StringBuffer "1,2,3," . Now , i have to send this "1,2,3," as a parameter String by removing the last character i.e. "1,2,3" StringBuffer has "1,2,3," . It has to be changed to "1,2,3" and then set in String which will be sent as parameter. thanks ...

43. StringBuffer    forums.oracle.com

45. StringBuffer and letter color    forums.oracle.com

46. Help with StringBuffer    forums.oracle.com

47. Stringbuffer probs    forums.oracle.com

48. Strange Error: Junk value getting inserted into StringBuffer    forums.oracle.com

Hello everyone, I have a method trying to send mails using SMTP server. This method calls the class 'EMail". In the constructor call, out of the many parameters, subject (StringBuffer) is one of them. If in the constructor call, if I pass the subject say like the one mentioned below: StringBuffer subject=new StringBuffer("Vijay is a gud boy"); EMail email=new EMail(..,..,..,subject,...); System.out.println("subject="+subject.toString()); ...

49. String vs StringBuffer vs StringBuild    forums.oracle.com

From my limited knowledge I can tell you that StringBuilder is more efficent than String because it is mutable. So it allows for easier joining of strings I haven't looked into StringBuffer, but I know whatever you can do with StringBuilder you can do with StringBuffer, but not vice-versa. So wherever possible use StringBuilder instead of String. As for StringBuffer it ...

50. StringBuffer    forums.oracle.com

thx for the help ..i had a class named easy in the folder.But even after changing the name of the class it didnt work.But it did work when i changed the name of the method.neways thanx fellows! But a few queries about String Buffer...in the above program.....when i append to s1 the value is stored and is printed in the parent ...

51. create virtual fields in StringBuffer    forums.oracle.com

thanks phdk , i didn't thought of it that way. i have code working by using indexof() but its lengthy & not convenient to change and i have to test for updated index, but for ease of working with my code & modifying in future, im looking for other way to it. im still new to java & i would look ...

52. StringBuffer    forums.oracle.com

Hi all Is there any code that will get the index of all occurences of a character in string one after another? Only code i can find is indexOf(char) to get index of first occurance and lastIndexOf(char) to get the last one. so if there is anything in between eg "sleeper" I would like to be able to replace all the ...

53. What is the difference between String, StringBuffer, StringBuilder?    forums.oracle.com

String is an immutable object meaning that once the object is created it cannot be modified, hence string concatenation is a process that creates many objects that are no longer required and hence collected by the garbage collector. This inefficiency lead to the introduction of the StringBuffer which can concatenate Strings much faster. As of Java 1.5 Sun introduced the StringBuilder ...

54. StringBuffer retains values after changing in a method    forums.oracle.com

Nice example... I'm partial to dogs and leashes myself. A leash with no dog ain't much use... and a dog without a leash (or a rego tag) is available to be GC'ed. Don't know how you'd go about passing a leash by value though... or explaining how lending your leash to a neighbour results in two distinct leashes on one dog, ...

55. StringBuffer    forums.oracle.com

Hi, I am very sorry, but from your posting it is hard to tell where the Exception occurs, and whether or not you e.g. initialize the StringBuffer instance. As stated in other postings, you should provide a small, compilable and runnable example of your problem to allow others to examine it. Your code fragment is not complete, so you may receive ...

56. NonAllocating StringBuffer Help    forums.oracle.com

1. Create a string, which is for example "Hello my name is John Smith." 2. Append the characters from this string into the StringBuffer. 3. Create a linked list and in the first node create an array, and then put each character into a slot in the array. 4. When the array is full, create a new node and in it ...

57. Difference between String and StringBuffer??    forums.oracle.com

Hi there, Actually what you know is most of the needed information. Objects of type String are immutable and read only, thus changing the String value will actually create a new Object in a new meomry location. Objects of type StringBuffer are mutable, thus changing the StringBuffer value will actually update the same Object in the meomry. Objects of type StringBuilder ...

58. get the StringBuffer values one by one    forums.oracle.com

59. StringBuffer    forums.oracle.com

60. Charset & StringBuffer    forums.oracle.com

62. String or StringBuffer???    forums.oracle.com

Rather than me, or other foremost authorities, telling you to use StringBuffer (actually, I would tell you to use StringBuilder), why not take that concatenation example, and write it once using String and once using StringBuilder. Time the results. This should only take a few minutes to do. Post your finding here.

63. Java-String vs StringBuffer Class    forums.oracle.com

String s1 = new String("Selvakumar"); // The String literal "Selvakumar" is passed in to make a new String with the same value which is then assigned to the String reference variable s1 String s2 = new String("Ravikumar"); // The String literal "Ravikumar" is passed in to make a new String with the same value which is then assigned to the String ...

65. String vs Stringbuffer    forums.oracle.com

66. reg: string and stringbuffer    forums.oracle.com

67. String (vs) StringBuffer    forums.oracle.com

/** * Class String is special cased within the Serialization Stream Protocol. * * A String instance is written intially into an ObjectOutputStream in the * following format: *

 * TC_STRING (utf String) * 
* The String is written by method DataOutput.writeUTF. * A new handle is generated to refer to all future references to the * string instance ...

68. StringBuffer: max dimensio    forums.oracle.com

Hi, I'm new on this forum, sorry for my english but I don't speak it very well. I have an application that write on a logfile when there somo information to store. But the problem is that use more time to write on the logfile (high numbers of write) that to execute all the application (that is more important). Now I ...

69. StringBuffer    forums.oracle.com

class test { static void m1(StringBuffer s1) { s1.append("2"); System.out.print(s1); } static void m2(StringBuffer s1) { s1 = new StringBuffer("3"); System.out.print(s1); } public static void main(String[] s) { StringBuffer s1 = new StringBuffer("1"); m1(s1); m2(s1); System.out.print(s1); }} Result: 12312 I could understand the output till 12 & 3 being the output of s1 at method m2. But print() in main,should be ...

70. StringBuffer    forums.oracle.com

71. how to know if a stringbuffer is empty    forums.oracle.com

72. How to remove White Space in StringBuffer?    forums.oracle.com

73. String and StringBuffer    forums.oracle.com

74. Performace of StringBuilder - StringBuffer - which one is better    forums.oracle.com

Javadoc mention that StringBuilder performance is better then that of a StringBuffer and whenever the class is used by a single thread , StringBuilder is faster . I tried creating a long StringBuilder and a long StringBuffer and compared the performace of both. If size >10000 , then the append method performs faster for StringBuffer rather then StringBuilder. Anyone has any ...

75. Needed help in sorting values in StringBuffer    forums.oracle.com

Hi, I will get a StringBuffer as an input which contains lines in different order, Please refer the below example "A,B,C,D, LINE_NO=1" "E,F,G,H, LINE_NO=8" "I,J,K,L, LINE_NO=6" "M,N,O,P, LINE_NO=3" All the above four lines will be in the StringBuffer based on the above order, I need to create a new StrinBuffer order by Line_No. Can anyone help me on this..?

76. Reader from StringBuffer    forums.oracle.com

The StringBuffer is NOT a data structure which was meant for streaming. It is just there to store and concate Strings. So your problem really is with your StringBuffer, and not the resulting Strings. I don't know your program, but maybe you can substitute your StringBuffer by something which is more appropiate for streaming.

77. Copying the value of a StringBuffer Object    forums.oracle.com

78. StringBuffer Problem    forums.oracle.com

Hi Can anybody te;ll me what is the problem with my code below StringBuffer finalvalue = new StringBuffer(); String responsevalue = null; while (rset2.next()) { responsevalue = rset2.getString("OptionTextDescription"); //finalvalue.append(responsevalue + "," ); // retVal.add(finalvalue); } finalvalue.append(responsevalue + "," ); System.out.println("*********************************"+ finalvalue); } The code above seems to not work at all and the appending is not happening. please let me know ...

79. StringBuffer question    forums.oracle.com

80. String and StringBuffer    forums.oracle.com

81. StringBuffer    forums.oracle.com

82. StringBuffer limit    forums.oracle.com

Hello, i have a StringBuffer sb. In my code, i "append" a page html in many lines. this html have some vars of Java (a form send this vars to my page) the problem: if the form have many caracteres, my page stop the proccess and still this: Title: value of form Title: title: option

83. StringBuffer constructer    forums.oracle.com

84. Tokenizing StringBuffer    forums.oracle.com

85. String and Stringbuffer    forums.oracle.com

86. String and Stringbuffer    forums.oracle.com

87. use of StringBuffer...    forums.oracle.com

Hi all, I am developing an application where in a huge amount of characters are created on the fly (by a JSP) using underlying business logic, just wanted to know if using a StringBuffer to temporarily store this data (may be 3-4 MB) at the server end is a feasible idea... Any workarounds or suggestions will be of great help... Regards ...

88. StringBuffer Problem.    forums.oracle.com

Hi, sorry for not providing details abt the same. When after emptying the stringbuffer I try to reuse it. All append functions works as normal, but I can;t able to cast that StringBuffer in string by using toString and I can;t able to find the character at a given position by charAt. It simply says that IndexOutOfBound.

89. String & StringBuffer    forums.oracle.com

Strings are immutable because it makes no sense to make them mutable. strings in real life are immutable: is the word "mutable" the same as the word "camera", only with the letters changed, or is it a new, different word? StringBuffer is mutable because otherwise it wouldn't work. you have to be able to add to it, and change it, by ...

90. StringBuffer    forums.oracle.com

I love it when noobs think they know better than the more experienced. Well that's understandable in some cases, but when they start to insist... Just recently I helped someone that was using StringTokenizer to split a comma-space separated String "foo, bar, baz". He had comma as the separator and didn't do a trim() on the tokens. Then he did basically ...

91. StringBuffer    forums.oracle.com

hi everyone I had a arraylist of strigbuffers in the jsp. Now i had to print all the elements of the arraylist i.e. stringbuffers which are stored in the arraylist but when i display it by converting them to javascript array they give following error Syntax Error Kindly help me to resolve this problem Thanks in advance

93. Alternative for StringBuffer    forums.oracle.com

Hi, In the DAO classes, we are using the StringBuffer to append various SQL statements based on the condition, hence there are so many append statements every where. Is there any other alternative we have public List getReport(String custType) { StringBuffer strBuffer = new StringBuffer(); strBuffer.append("Select * from Customer cust"); if(custType.equals("HOME)){ strBuffer.append("where ......"); } else if(custType.equals("BUSINESS){ strBuffer.append("where ......"); )else if(custType.equals("NONE"){ strBuffer.append("where ...

94. String and StringBuffer    forums.oracle.com

} } just run the sample code; in java every object will have a unique hashcode. the hashcode at line 3 is different from hashcode at line 5 . when ever we try to change the state of the string(immutable) object it will create a new object. but in case of stringbuffer(mutable object) the new object is not created. ...... meher. ...

95. does anyone still use the stringbuffer class anymore    forums.oracle.com

Yeah, the whole reason for StringBuilder has always seemed rather specious to me. It's only useful in a single-threaded context (which is the only time I've ever used either SB class as far as I can recall), but, as you say, uncontested syncing is very fast--probably an order of magnitude faster than building up a char[]--so what real benefit is achieved ...