All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.expr.HolderCharsScanner Maven / Gradle / Ivy

There is a newer version: 5.2.7
Show newest version
package org.onetwo.common.expr;

public class HolderCharsScanner {
	
	public static HolderCharsScanner holder(String source){
		return new HolderCharsScanner(source);
	}
	
	public static interface RelacementProvider {
		public String getHolderValue(int index);
	}
	
	final private String holderChars;

	private HolderCharsScanner(String source) {
		super();
		this.holderChars = source;
	}
	
	public String parse(String source, RelacementProvider provider){
		int varIndex = 0;
		StringBuilder sb = new StringBuilder();
		int beginIndex = source.indexOf(holderChars);
		int off = 0;
		int size = holderChars.length();
		while(beginIndex != -1){
			sb.append(source.substring(off, beginIndex));
			String val = provider.getHolderValue(varIndex++);
			sb.append(val);
			
			off = beginIndex + size;
			beginIndex = source.indexOf(holderChars, off);
		}
		String tail = source.substring(off, source.length());
		sb.append(tail);
		
		return sb.toString();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy