JosephG
JosephG

Reputation: 3253

Get all occurrences of a string contained within a String

In Java, I need to find all occurrences of a String inside of a String.

eg.

String myString;

myString = "Random_XML_Stuff_Here <tag k="name" v="Example Road"/> 
More_Random_XML_Stuff <tag k="name" v="Another name"/> More_XML_Stuff" Etc...

So I need to be able go grab the contents off all the Road names. In the first example, I need to be able to set a String to "Example Road".

Pseudo-Code:

String streets = "";
while(more occurrences of street names exist)
{
    streets = streets + "," + (street.occurrence of street name);
}

In the above example, the string would have the contents "Example Road, Another name".

Upvotes: 0

Views: 197

Answers (4)

pramod Biradar
pramod Biradar

Reputation: 1

ANswering following question using python:

inside a string find all characters with its occurrences eg: I/P str='aababbdf'
O/P: [('a', 2), ('b', 1), ('a', 1), ('b', 2), ('d', 1), ('f', 1)]

i=j=total=0;

data=[];
for i in range (0,len(str)):
    count=0;
    i = j;
    if( total != len(str)):
        for j in range (i,len(str)):            
            if str[i] == str[j]:
                count=count+1;
                total = total +1;
                char=str[i];
            else:
                break;

        data.append((char,count));  

print("\n data",data);

Upvotes: 0

michaeltang
michaeltang

Reputation: 2898

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class welcome {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String text = "Random_XML_Stuff_Here <tag k=\"name\" v=\"Example Road\"/> More_Random_XML_Stuff <tag k=\"name\" v=\"Another name\"/> More_XML_Stuff";
        String road_string = GetRoadString(text);
        System.out.println(road_string);

    }
    static String GetRoadString(String text)
    {
        Pattern pattern = Pattern.compile("<tag\\s+k=\"name\"\\s+v=\"(.*?)\"/>");
        Matcher matcher = pattern.matcher(text);
        // using Matcher find(), group(), start() and end() methods
        String road_string = "";
        while (matcher.find()) {
            road_string = road_string + matcher.group(1)+ ",";
        }
        return road_string.substring(0, road_string.length()-1);
    }
}

    enter code here

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201447

You could use something like this String[] parseValue(String) function

public static String[] parseValue(String in) {
  String openTag = "<tag k=\"name\" v=\"";
  int p1 = in.indexOf(openTag);
  java.util.List<String> al = new java.util.ArrayList<String>();
  while (p1 > -1) {
    int p2 = in.indexOf("\"/>", p1 + 1);
    if (p2 > -1) {
      al.add(in.substring(p1 + openTag.length(), p2));
    } else {
      break;
    }
    p1 = in.indexOf(openTag, p2 + 1);
  }
  String[] out = new String[al.size()];
  return al.toArray(out);
}

public static void main(String[] args) {
  String myString = "Random_XML_Stuff_Here <tag k=\"name\" v=\"Example Road\"/> "
      + "More_Random_XML_Stuff <tag k=\"name\" v=\"Another name\"/>";
  System.out.println(java.util.Arrays
      .toString(parseValue(myString)));
}

which outputs

[Example Road, Another name]

Upvotes: 1

Chthonic Project
Chthonic Project

Reputation: 8366

Since you have a fixed XML format, you can use a regular expression match to find the road names mentioned.

Pattern roadNamePattern = Pattern.compile("v=\"(.*?)\"/>");
Matcher matcher = roadNamePattern.matcher(xmlString);
while (matcher.find()) {
    String roadName = matcher.group(1);
}

Upvotes: 0

Related Questions