R
R
roman38472014-04-22 13:18:29
Java
roman3847, 2014-04-22 13:18:29

Java string - how to loop through a string?

For example, I have a string: abcd*tt()uyuy*u()yuy
Is it possible to replace *tt() and *u() with "edit" and "reduce".
The whole problem is that the order in which these constructions occur is important for the task, something like we start a loop over the line and if we met *tt (), then we need to perform one action, if we met * u (), then we perform another action, if none of the predefined constructions is met, then we simply go further along the line. Whether it is possible to implement it somehow?
Ps can anyone type the "skeleton" of this cycle, and then I'll figure it out myself.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey Vershinin, 2014-04-22
@roman3847

String parse(String s) {
  String str = new String(s);
  final int tt_len = 4, u_len = 3;
  final String tt = "tt()", u = "u()", tt_repl = "edit", u_repl = "reduce";
    boolean tt_mode = false, u_mode = false;

  String tmp;
  
    for(int i = 0; i < str.length(); i++) {
    tmp = str.substring(i, 5);
    
    if(tmp.startsWith(tt)) {
      tt_mode = true;
      str.replaceFirst(tt, tt_repl);
      i += tt_len;
      
      if(u_mode) {
      
      }
    } else if(tmp.startsWith(u)) {
      u_mode = true;
      str.replaceFirst(u, u_repl);
      i += u_len;
      
      if(tt_mode) {
      
      }
    }
    }
}

P
pigah, 2014-04-22
@pigah

protected String replaceString(String aSearch, String aFind, String aReplace) {
    String result = aSearch;
    if (result != null && result.length() > 0) {
        int a = 0;
        int b = 0;
        while (true) {
            a = result.indexOf(aFind, b);
            if (a != -1) {
                result = result.substring(0, a) + aReplace + result.substring(a + aFind.length());
                b = a + aReplace.length();
            } else
                break;
        }
    }
    return result;
}

Code for searching and replacing a substring, instead of replacing, you can do whatever you want

A
Aksndr, 2014-04-22
@Aksndr

Well, here's an option:

public static void main (String args[]){

        HashMap<String,String> map = new HashMap<String, String>();
        map.put("*tt()","edit");
        map.put("*u()","reduce");
        String sourceString = "abcd*tt()uyuy*u()yuy";

        String result = replaceString(sourceString, map);
        System.out.println(result);
    }

    public static String replaceString(String sourceString, HashMap<String,String> map){
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry)it.next();
            String patternString = shieldSpecialSymboll((String)pair.getKey());
            sourceString = sourceString.replaceAll(patternString, (String)pair.getValue());            
            it.remove();
        }
        return sourceString;
    }

    private static String shieldSpecialSymboll(String key) {
        char[] sp = {'.','^','$','*','+','?'};
        for (char ch : sp){
            if (key.startsWith(String.valueOf(ch)))
                key = "\\"+key;
        }
        return key;
    }

G
GM2mars, 2014-04-22
@GM2mars

If I understand you correctly:

var str='abcd*tt()uyuy*u()yuy';
var length=str.length+1;
var reg1=/^[a-z0-9]*(\*tt\(\)){1}/i;
var reg2=/^[a-z0-9]*(\*u\(\)){1}/i;
var cut=0;

for (var i=1; i<length; i++) {
    var temp=str.substring(cut, i);
    if (reg1.test(temp)) {
        console.log('edit');
        cut=i;
    }
    if (reg2.test(temp)) {
        console.log('reduce');
        cut=i;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question