• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieve certain string from StringBuilder  RSS feed

 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to retrieve a string from a StringBuilder if we know the index of the string we want to retrieve? i mean something similar to list.get().
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't understand you fully, but there is always a toString() method to get the string from a StringBuilder/StringBuffer.
 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean something like this: say i have a string like "if(c==1){a=x-3/23;\r\n z=23+newVariable;}" now i have mapped the indexes of every "=" sign and "if" that appear in that string because i need them for later. I split that string and added it to a List which now looks something like "[if, (, c, =, =, 1, ), ....]" then looped through the list and added every item to a stringBuilder and grabbed the index of the above mentioned "=" and "if" like this: Now at some point in my application i need to check what is in front of every "=" sign and do something according to that. Normally i would just do list.get(indexOfStrings) but since i got the index the way i did I am not able to do that because get() method will return the position when i need the index of the element (it will end up with the list having less elements than the actual value of indexOfString). That's why i'm looking for a way to do this using stringBuilder maybe. Open to any other suggestions of course. Hope i cleared my question up a bit and i didn't make it more confusing (i tend to do that )
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in other words, first you decided to use a StringBuilder as a data structure to store some data, and now you're having trouble getting data back out of that data structure? That's because String and StringBuilder are really poor choices for storing structured data. As you are now finding out. So stop and choose some more suitable data structure.
 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:So in other words, first you decided to use a StringBuilder as a data structure to store some data, and now you're having trouble getting data back out of that data structure? That's because String and StringBuilder are really poor choices for storing structured data. As you are now finding out. So stop and choose some more suitable data structure.
That is one way to learn :).

Any suggestions on what data structures i should use to achieve what i need Paul?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Vlad wrote:Any suggestions on what data structures i should use to achieve what i need Paul?


If I had any idea what you needed, I might have some suggestions. But from what you posted, I have no idea what you're trying to do.
 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically what i am trying to do is: 1. take some input from the user. this input will come under the form of a lot of if/else/switch/case combined with some simple math operations (the string i posted above is a very simple example).
2. if the input contains certain strings(i call them references), for example something like "AGE_Y" or "PROPERTY_LENGTH" (there are a lot of these also) , i grab the first occurrence of each of these and turn it into a variable to which i will add some extra code(in order for it to be read by another application) and insert it right above where it first appeared in the text. i'll post a clearer example lower.
3. once i created those variables i will replace all the occurrences of the references with the name of the variable

Now up to this point i''ve managed to do this properly, except that the insertion goes wonky, and that's why i need to do what i described in the above post. For example if i find x=PROPERTY_LENGTH; as the first occurrence of the reference , i already know the index of PROPERTY_LENGTH and of the = sign closest to it(made a method to help out with that), but i need to grab the value in front of the "=" sign in order to properly insert the newly created variable. for example , if it was in a list and the index of "=" would be , a variable called "index", i would do something like: which would grab me string "x"(who's length can obviously vary) , and after that i would simply do a

oh and for a clearer example of a string: this is a very simple example, but an accurate one.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you're defining a language. So your first step will be to define the grammar. Then you'll have to figure out the nature of the language--is it just a simple expression parser, or something more complex? This is not a fuzzy, subjective question. There are different kinds of languages with formal definitions as to what makes one or the other. I'm not familiar with these details, but it's something you'll want at least a passing understanding of, so that you can decide what tools you'll use to turn that grammar into a parser.

This is a very high-level, oversimplified picture, but the key point is, you can't just say, "find the ==s and ifs in the string...".
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with @Jeff, you are defining a language parser. This is covered in any good undergraduate CS program, typically in the fourth year.

Google for "domain specific language" for links, tools, etc.
 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the replies. At this point i pretty much have set some rules by which to define the grammar. As you said Jeff, the question i posted above is
a very high-level, oversimplified picture
but at the moment that is what i am struggling with .

@Pat: sorry, but what exactly do you mean by undergraduate CS program? Also , i will start looking into domain specific language so thanks.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Vlad wrote:@Pat: sorry, but what exactly do you mean by undergraduate CS program? Also , i will start looking into domain specific language so thanks.


Undergraduate Computer Science program. The "science" of computers as taught in a four year college to 18-22 year olds. At least that is typical in the US.

Parsers and compilers are a critical part of any complete CS program, because (1) they are fundamental to so many things we do and (2) the require a full understanding of programming and data structures.

However, these are not "beginning" topics. They are usually taught in the 3rd or 4th year of a four year program.

A course in compilers is also typically required for a MS-CS (Master of Science, Computer Science) in many US universities.

 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing that up. I wish I would have had a course like that in college , would have been helpful . I understand the basics of compilers and parser , but I imagine a deeper understanding would have helped a lot right about now.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that there are tools to help you with some of the more tedious, error-prone steps. For instance, there are tools like lex and yacc in *nix that can be used to generate a parser (given the grammar in BNF, IIRC), and I think there's something similar for Java, though I can't remember what it's called. (Javacc maybe?) Googling for java parser generator or java yacc equivalent or somesuch should set you down the right path.

I'm not sure if you're interested in doing the whole thing yourself as an academic exercise, or if you just want the final result for practical use, or somewhere in between, so you'll have to investigate to see which of those tools are useful to you.

Additionally, you may want to look into Scala. It's a programming language that supports most or all of Java's syntax, and gets compiled to Java bytecodes that can run in JVM. It has additional syntactical features that are supposed to make it good for developing domain-specific languages, though I haven't actually done this myself, so I can't comment on this feature.

And, finally, on the opposite end of the spectrum, if you just need the ability to execute Strings like "if (x == 1) { a = 2; }" and are not locked into a specific syntax, then you may find Java's scripting hooks for JavaScript useful, or the beanshell library, or Groovy, all of which let you just pass a String and tell it "evaluate this."

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!