java字符串拼接指定内容-左右组合(left/right padding)

 

今天在处理ID时,又一次碰到了字符串的左右拼接问题。相信很多人都有经历。

我们还是熟悉下需求,还是那句话。需求相同才是可用的解决方案。

我们需要在某个字符串的左边填充0,总长度为10位,或者右边填充0,总长度为0,或者,前后的内容固定,中间需要填充0.

 

String没有left padding ,right padding的函数。。那只有自己写了。。

代码性能没有测试,但是自己感觉还行。

 

上代码。(英语较烂,凑合看)

 

 

/**
	 * get left padding String with specification char for existed suffix
	 * for example,if need left padding zero for some String,
	 * like left padding 0 to 12,and need total length 10,
	 * then the result will be 0000000012. 
	 * 
	 * @param suffix
	 * 			the suffix for return String
	 * @param fill
	 * 			the fill char for String
	 * @param totalLength
	 * 			the total length need for composite String
	 * @return
	 */
	public static String getLeftPadding(String suffix,char fill,int totalLength){
		
		return getFillString("",suffix,fill,totalLength);
	}
	
	/**
	 * get right padding String with specification char for existed prefix
	 * for example,if need left padding zero for some String,
	 * like right padding 0 to A,and need total length 10,
	 * then the result will be A000000000. 
	 * 
	 * @param prefix
	 * 			the prefix for return String
	 * @param fill
	 * 			the fill char for String
	 * @param totalLength
	 * 			the total length need for composite String
	 * @return
	 */
	public static String getRightPadding(String prefix,char fill,int totalLength){
		
		return getFillString(prefix,"",fill,totalLength);
	}
	
	/**
	 * get char fill composite String for existed prefix or suffix
	 * for example,if need left padding zero for some String,
	 * like padding 0 to 12,and need total length 10,
	 * then the result will be 0000000012. what's more,
	 * if add a prefix for this composite String,like A, 
	 * then then return String will be A000000012.
	 * 
	 * @param prefix
	 * 			the prefix for return String
	 * @param suffix
	 * 			the suffix for return String
	 * @param fill
	 * 			the fill char for String
	 * @param totalLength
	 * 			the total length need for composite String
	 * @return
	 */
	public static String getFillString(String prefix,String suffix,char fill,int totalLength){
		int c = 0;
		int needLength = totalLength - (prefix.length()+suffix.length());
		
		StringBuilder sb = new StringBuilder();
		sb.append(prefix);
		while(c < needLength){
			sb.append(fill);
			c++;
		}
		sb.append(suffix);
		return sb.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(getFillString("A","1",'0',10));
		System.out.println(getLeftPadding("1",'0',10));
		System.out.println(getRightPadding("A",'0',10));
	}

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为AI语言模型,我无法为您提供完整的编程解决方案,但我可以给您提供一些思路和代码片段。 1. 思路 要计算字符串“8-9/3”,我们需要将其转化为数学表达式,并进行计算。具体来说,我们可以采用以下步骤: 1)将字符串转化为表达式树; 2)对表达式树进行遍历,计算出表达式的值。 2. 代码片段 以下是Java中实现表达式树的代码片段,可以供您参考: ``` //定义表达式树的节点类 class TreeNode { String value; TreeNode left; TreeNode right; public TreeNode(String value) { this.value = value; this.left = null; this.right = null; } } //将字符串转化为表达式树 public TreeNode buildExpressionTree(String[] expression) { Stack<TreeNode> stack = new Stack<>(); for (int i = 0; i < expression.length; i++) { if (isOperator(expression[i])) { TreeNode node = new TreeNode(expression[i]); node.right = stack.pop(); node.left = stack.pop(); stack.push(node); } else { stack.push(new TreeNode(expression[i])); } } return stack.pop(); } //判断是否为运算符 public boolean isOperator(String s) { return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/"); } ``` 接下来是计算表达式树的代码片段: ``` //计算表达式树 public int calculateExpressionTree(TreeNode root) { if (root == null) { return 0; } if (root.left == null && root.right == null) { return Integer.parseInt(root.value); } int left = calculateExpressionTree(root.left); int right = calculateExpressionTree(root.right); if (root.value.equals("+")) { return left + right; } else if (root.value.equals("-")) { return left - right; } else if (root.value.equals("*")) { return left * right; } else { return left / right; } } ``` 最后,我们可以将以上代码片段组合起来,实现对字符串“8-9/3”的计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值