实际开发中String常用方法记录startswith与replaceAll

在实际项目中,String的应用非常广泛频繁,我记录一下现在在操作项目中经常使用到的String方法

1.startswith
进入String.startswith方法可以看到以下代码

   /**
     * Tests if this string starts with the specified prefix.
     *
     * @param   prefix   the prefix.
     * @return  {@code true} if the character sequence represented by the
     *          argument is a prefix of the character sequence represented by
     *          this string; {@code false} otherwise.
     *          Note also that {@code true} will be returned if the
     *          argument is an empty string or is equal to this
     *          {@code String} object as determined by the
     *          {@link #equals(Object)} method.
     * @since   1. 0
     */
    public boolean startsWith(String prefix) {
        return startsWith(prefix, 0);
    }

这个方法注释大概意思就是如果传入的字符串字符序列的前缀符合你定义的前缀参数那么就给一个true的结果,要注意如果参数为空字符串或等于这个属性确定的{@code String}对象{@link # =(对象)}的方法也会为true。
写个列子

public class Test {
    public static void main(String[] args) throws ParseException {
    String a= "DL04P00610097,DL04P00610096";
   boolean s=a.startsWith("");
        System.out.println(s);
    }
}

执行的结果为:

true

这里的我在调用a.startsWith方法时没有赋予它任何参数,无论我如何改变a字符串,最终的结果始终为true。
一般开发中使用该方法都是为了判断传入的字符串是否带有业务上所设定的前缀,比如我前端传入了
"L04P00610097,L04P00610096 "我想要判断如果该参数如果前缀不是L就不执行方法就可以这样写

public class Test {
    public static void main(String[] args) throws ParseException {
    String a= "L04P00610097,L04P00610096";
    if (!a.startsWith("L")){
        System.out.println("这种类型的参数不可以放进去"); ;
    } else {
        System.out.println("就只有这种类型的参数可以放进去");
    }
    }
}

结果

就只有这种类型的参数可以放进去

如果我执行的对象前缀不为L就是以下这种情况

public class Test {
    public static void main(String[] args) throws ParseException {
    String a= "DL04P00610097,L04P00610096";
    if (!a.startsWith("L")){
        System.out.println("这种类型的参数不可以放进去"); ;
    } else {
        System.out.println("就只有这种类型的参数可以放进去");
    }
    }
}

结果

这种类型的参数不可以放进去

总结:.如果startswith(" “)这种情况,字符串对象无论是什么都为true
如果字符串对象前缀与startswith(” 前缀")不符合,则返回false
利用好这个方法可以在实际开发中完成很多业务判断
2.replaceAll方法
进入这个方法先看看

 /**
     * Replaces each substring of this string that matches the given <a
     * href="../util/regex/Pattern.html#sum">regular expression</a> with the
     * given replacement.
     *
     * <p> An invocation of this method of the form
     * <i>str</i>{@code .replaceAll(}<i>regex</i>{@code ,} <i>repl</i>{@code )}
     * yields exactly the same result as the expression
     *
     * <blockquote>
     * <code>
     * {@link java.util.regex.Pattern}.{@link
     * java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link
     * java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link
     * java.util.regex.Matcher#replaceAll replaceAll}(<i>repl</i>)
     * </code>
     * </blockquote>
     *
     *<p>
     * Note that backslashes ({@code \}) and dollar signs ({@code $}) in the
     * replacement string may cause the results to be different than if it were
     * being treated as a literal replacement string; see
     * {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}.
     * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
     * meaning of these characters, if desired.
     *
     * @param   regex
     *          the regular expression to which this string is to be matched
     * @param   replacement
     *          the string to be substituted for each match
     *
     * @return  The resulting {@code String}
     *
     * @throws  PatternSyntaxException
     *          if the regular expression's syntax is invalid
     *
     * @see java.util.regex.Pattern
     *
     * @since 1.4
     * @spec JSR-51
     */
    public String replaceAll(String regex, String replacement) {
        return Pattern.compile(regex).matcher(this).replaceAll(replacement);
    }

注释有点多我大概讲一下,意思就是传入一个String类型的正则表达式或者参数,以及一个String类型的代替参数
regex是指正则表达式或者参数,replacement是指代替的参数
这个功能经常用于把前端传进来的参数改变成符合业务上的样式
写个例子:

public class Test {
    public static void main(String[] args) throws ParseException {
        String a = "DL04P00610097,L04P00610096";
        String s=a.replaceAll("D", "','");
        System.out.println(s);
    }
}

这个例子是指把a对象里的D替换成‘,’
执行结果如下:

','L04P00610097,L04P00610096

实际业务中在举个例子
当前我所做的一个业务需要用一个多个查询,要在前端传入DL04P00610097,L04P00610096查出它所对应的数据
sql是:

 <select id="getExData" parameterType="java.util.HashMap" resultType = "Map">
    select t.*,v.itemname,v.energyid,v.energytypeid,v.locateid from T_RM_CALPOINT_EX_VALUE t left join T_RM_CALPOINT v 
	on t.itemid = v.itemid 
	<where>
	  <if test="clock!=null and clock!=''">  
		and t.CLOCK &gt;= #{clock}
	  </if>
	  <if test="clocke!=null and clocke!=''">  
		and t.CLOCK &lt;= #{clocke}
	  </if>
	  <if test="itemid!=null and itemid!=''">  
		and t.ITEMID in (${itemid})
	  </if>
	  <if test="extype!=null and extype!=''">  
		and t.EXTYPE = #{extype}
	  </if>
	  <if test="maxvalue!=null and maxvalue!=''">  
		and t.MAXVALUE = #{maxvalue}
	  </if>
	  <if test="minvalue!=null and minvalue!=''">  
		and t.MINVALUE = #{minvalue}
	  </if>
	  <if test="apportvalue!=null and apportvalue!=''">  
		and t.APPORTVALUE = #{apportvalue}
	  </if>
	  <if test="befvalue!=null and befvalue!=''">  
		and t.BEFVALUE = #{befvalue}
	  </if>
	  <if test="curvalue!=null and curvalue!=''">  
		and t.CURVALUE = #{curvalue}
	  </if>
	  <if test="createtime!=null and createtime!=''">  
		and t.CREATETIME = #{createtime}
	  </if>
	  <if test="energyid!=null and energyid!=''">  
		and v.ENERGYID in (${energyid})
	  </if>
	  <if test="energytypeid!=null and energytypeid!=''">  
		and v.ENERGYTYPEID = #{energytypeid}
	  </if>
	  <if test="locateid!=null and locateid!=''">  
		and v.LOCATEID in (${locateid})
	  </if>
	</where>
	order by t.clock, t.itemid
  </select>

如果不做任何处理,前端传入参数为:DL04P00610097,DL04P00610096
放入sql中是一个字符串“DL04P00610097,DL04P00610096”这样就会出现sql错误,
所以咱们要把前端传入参数利用replaceAll方法修改成sql可以看得懂得形式

 if(!itemid.startsWith("'")){
                itemid = "'" + itemid.replaceAll(",", "','").replaceAll(",", "','") + "'";
            }
            parmas.put("itemid", itemid);

先利用startsWith判断参数前缀没有( ’ ),接着把获得的参数(DL04P00610097,DL04P00610096)通过replaceAll修改
修改后的效果为 ‘DL04P00610097‘,’DL04P00610096’
这样的话sql就可以执行查询任务了。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值