zoukankan      html  css  js  c++  java
  • 2.12 指定缩进

    
    package main
    
    import (
    	"fmt"
    	"strconv"
    	"strings"
    	"unicode"
    )
    
    func main() {
    
    	text := "Hi! Go is awesome."
    	text = Indent(text, 6)
    	fmt.Println(text)
    
    	text = Unindent(text, 3)
    	fmt.Println(text)
    
    	text = Unindent(text, 10)
    	fmt.Println(text)
    
    	text = IndentByRune(text, 10, '.')
    	fmt.Println(text)
    
    }
    
    // Indent indenting the input by given indent and rune
    func IndentByRune(input string, indent int, r rune) string {
    	return strings.Repeat(string(r), indent) + input
    }
    
    // Indent indenting the input by given indent
    func Indent(input string, indent int) string {
    	padding := indent + len(input)
    	return fmt.Sprintf("% "+strconv.Itoa(padding)+"s", input)
    }
    
    // Unindent unindenting the input string. In case the
    // input is indented by less than "indent" spaces
    // the min of this both is removed.
    func Unindent(input string, indent int) string {
    
    	count := 0
    	for _, val := range input {
    		if unicode.IsSpace(val) {
    			count++
    		}
    		if count == indent || !unicode.IsSpace(val) {
    			break
    		}
    	}
    
    	return input[count:]
    }
    
    /*
          Hi! Go is awesome.
       Hi! Go is awesome.
    Hi! Go is awesome.
    ..........Hi! Go is awesome.
    
    */
    
    
  • 相关阅读:
    事务 TRANSACTION
    SQLServer 数据库镜像+复制切换方案
    微软 codeplex 团队
    codeplex http://metrotoolkit.codeplex.com/
    CodeFlex AutoUpdate
    微软学习网站
    C# 关闭窗体立即停止进程
    反射方法关闭窗体报错的解决方法
    params修饰符
    c# 条形码(求指教)
  • 原文地址:https://www.cnblogs.com/zrdpy/p/8620715.html
Copyright © 2011-2022 走看看