• Post Reply Bookmark Topic Watch Topic
  • New Topic

Splitting/replacing characters twice  RSS feed

 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Some portion of text file:
id: activiti$3450325 , name isakymoprojektas:isakymoRengimas , properties: 27 , workflow: activiti$3424152 , path: activiti$3424152
id: activiti$3450914 , name paraiskuvaldymas:vizavimas , properties: 22 , workflow: activiti$3448140 , path: activiti$3450882
id: activiti$3453262 , name isakymoprojektas:isakymoRengimas , properties: 27 , workflow: activiti$3429484 , path: activiti$3429484
id: activiti$3453953 , name familiar:approvedTask , properties: 23 , workflow: activiti$3438339 , path: activiti$3438339
id: activiti$3454618 , name laisvosformosuzduotis:executeTask , properties: 19 , workflow: activiti$3454501 , path: activiti$3454580
id: activiti$3456259 , name paraiskuvaldymas:paraiskospateikimas , properties: 18 , workflow: activiti$3448140 , path: activiti$3448140

Text file consist of 1800 lines.
If I do replacement one time- its fine. I want to to do replacement twice. I meant - extract particular portion from the middle, while with one-action I cant do it.




This removes some text. But I need to remove few parts.

Removed        Want to be removed
id: activiti$3450325 , name isakymoprojektas:isakymoRengimas , properties: 27 , workflow: activiti$3424152 , path: activiti$3424152


 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same questions applies for:



With this one I want to delete letters from like 0-19 and 24-28. While keeping 20-23 letters (in the middle). Operator '&&' dont works here.




Also about split method, but I believe this is the same case.

 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please have a look at the delete and split methods and see what parameters they take. Do they take those parameters?

In the split method, you shou‍ld be able to create a regular expression that includes : and whichever other character you want to use. It is not easy however. In the case of a StringBuilder, most methods return the same object so you can write
myStringBuilder.append(...).insert(...).delete(...);
or similar.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:. . . Operator '&&' dont works here. . . .
Please have a look at the Java® Language Specification (=JLS) and see what sort of operands && takes. Then you will know whether it works with ints. Also find out what the & operator does when applied to ints and work out how badly it will go wrong if you try it here
Beware: the JLS can be difficult to read.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A statement like this: ob.replaceAll (", name" , "") &&( "path:", "") 
is not valid Java syntax.

Presumably you want to call the method twice, with different parameters each time. There is no syntax with "&&" to do that like this.

Note that replaceAll returns a string. You can call other methods on the string that is returned. You can do that explicitly:

Or you can do it on one line, by chaining the method calls:


See this topic for more info: method chaining
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Regarding the original question.... Since the replaceAll() method returns a string object, one option is to method chain the two operations. And two, since the replaceAll() method takes a regular expression, another option is to combine the two regexes, via regex with the alternation operator.

As for the "&&" being used -- where did you learn that? Perhaps, it would be a good idea to go back to the source material, to see how you got confused? Or are you simply guessing based on what is available in the language?

Henry
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Really good info.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!