Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.aliuge.crawler.extractor.selector.action.string; import org.aliuge.crawler.extractor.selector.action.StringSelectorAction; import org.apache.commons.lang3.StringUtils; /** * @author * @date 2014911 * @desc ?Action<br> * "4,4"??4.<br> * "4"4?? */ public class StringSubAction extends StringSelectorAction { /** * ?? */ int pos = 0; /** * ? */ String spos = ""; /** * ? */ int lenth = 0; /** * "4,4"??4.<br> * "4"4?? * @param subExpression */ public StringSubAction(String subExpression) { if (StringUtils.isNotBlank(subExpression)) { String[] ss = StringUtils.split(subExpression, ","); if (ss.length == 1) { if (StringUtils.isNumeric(ss[0])) { this.pos = Integer.parseInt(ss[0]); } else { this.spos = ss[0]; } } else if (ss.length == 2) { if (StringUtils.isNumeric(ss[0])) { this.pos = Integer.parseInt(ss[0]); } else { this.spos = ss[0]; } this.lenth = Integer.parseInt(ss[1]); } } } /** * ??? */ @Override public String doAction(String content) { if (StringUtils.isNotBlank(content)) { if (StringUtils.isNotBlank(spos)) { this.pos = content.indexOf(spos) + spos.length(); } if (this.lenth == 0 && this.pos > 0) { content = StringUtils.substring(content, this.pos); } else if (this.lenth > 0 && this.pos >= 0) { content = StringUtils.substring(content, this.pos, this.pos + this.lenth); } return content; } return ""; } public static void main(String[] args) { String string = "234dfs454#$%gasfdjlkas"; StringSubAction actiong = new StringSubAction("3,4"); System.out.println(actiong.doAction(string)); StringSubAction action2 = new StringSubAction("gas,4"); System.out.println(action2.doAction(string)); } }