It's not a secret anymore!*
The moose likes Beginning Java and the fly likes find a word in a text and take a part of a text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "find a word in a text and take a part of a text" Watch "find a word in a text and take a part of a text" New topic
Author

find a word in a text and take a part of a text

Dim Dioannou
Greenhorn

Joined: May 20, 2011
Posts: 6
I want to find a word in a text file and then to take a part of it and to
save it in a String.
for example I have this part of text
<rdf:RDF
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'

and when I find the word ''xmlns:'' I want to take the word after : till =
(rdf in this case) and to put it in a string.

Any idea how to do it?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

is the text always going to have that same pattern of "xmlns:"? If you you could use regular expressions to find this pretty easily.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Welcome to the Ranch!

Your question sounds a lot like this one that was asked yesterday. I posted an answer there that might be useful to you.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Harlin Seritt
Greenhorn

Joined: Apr 22, 2008
Posts: 8
You can do something like this in a tight:

String text = "<rdf:RDF
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'";

String del1 = "xmlns:";

String newString = text.split(del1)[1].split("=")[0];

System.out.println(newString);

But a regular expression would be the way to go.
kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

Dim Dioannou wrote:I want to find a word in a text file and then to take a part of it and to
save it in a String.
for example I have this part of text
<rdf:RDF
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'

and when I find the word ''xmlns:'' I want to take the word after : till =
(rdf in this case) and to put it in a string.

Any idea how to do it?



You may try this.

Dim Dioannou
Greenhorn

Joined: May 20, 2011
Posts: 6
@kumarjit banerjee thank you
this is what i need

but can you explain the code in line 38, please?
kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

Dim Dioannou wrote:@kumarjit banerjee thank you
this is what i need

but can you explain the code in line 38, please?



Hi


as.add(String.copyValueOf(m.group().toCharArray(), 6, m.group().length()-7));


This line copies the matched string (m.group returns the total matched string) from the characters of at position 6 to total length-7, this because

xmlns: length=6
= length=1

So the total length =7

Hence the code.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
All the approaches suggested so far are horribly brittle. It's perfectly possible for the XML to change in various valid ways that would break your code. Regexps simply are not capable of dealing with that. If you're handling XML, you need to use XML/DOM APIs.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: find a word in a text and take a part of a text