golang 进制转换_java string转integer

golang 进制转换_java string转integer1-Youmaywriteyourconversionfunction(Fastest):funcString(nint32)string{buf:=[11]byte{}pos:=len(buf)i:=int64(n)signed:=i<0ifsigned{i=-i}for{pos–buf[pos],i=’0’+byt

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

1- You may write your conversion function (Fastest):

func String(n int32) string {
    buf := [11]byte{}
    pos := len(buf)
    i := int64(n)
    signed := i < 0
    if signed {
        i = -i
    }
    for {
        pos--
        buf[pos], i = '0'+byte(i%10), i/10
        if i == 0 {
            if signed {
                pos--
                buf[pos] = '-'
            }
            return string(buf[pos:])
        }
    }
}

2- You may use fmt.Sprint(i) (Slow)
See inside:

// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
func Sprint(a ...interface{}) string {
    p := newPrinter()
    p.doPrint(a)
    s := string(p.buf)
    p.free()
    return s
}

3- You may use strconv.Itoa(int(i)) (Fast)
See inside:

// Itoa is shorthand for FormatInt(int64(i), 10).
func Itoa(i int) string {
    return FormatInt(int64(i), 10)
}

4- You may use strconv.FormatInt(int64(i), 10) (Faster)
See inside:

// FormatInt returns the string representation of i in the given base,
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
// for digit values >= 10.
func FormatInt(i int64, base int) string {
    _, s := formatBits(nil, uint64(i), base, i < 0, false)
    return s
}

以上内容出自: https://stackoverflow.com/questions/39442167/convert-int32-to-string-in-golang.

本人自用代码:

func Test_conver(t *testing.T) {
	var stairName string
	s := "test886400"
	id := int32(886400)
	iDStr := strconv.FormatInt(int64(id), 10)
	if strings.Contains(s, iDStr) {
		stairName = strings.TrimSuffix(s, iDStr)
	}

	fmt.Println("stairName:", stairName)
}

效果如下:
原创截图

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/193161.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ubuntu pycharm激活 3月最新注册码「建议收藏」

    ubuntu pycharm激活 3月最新注册码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月14日
    180
  • 查看gcc的版本

    查看gcc的版本查看gcc版本 命令:debian:dpkg-lgccredhat:rpm-qa|grepgcc

    2022年6月26日
    70
  • 银行家算法C语言版「建议收藏」

    银行家算法C语言版「建议收藏」#include<stdio.h>#include<stdlib.h>#include<conio.h>#definem50#definetrue1#definefalse0intno1;//进程数intno2;//资源数intr;intallocation[m][m],need[m][m],available[m],max[m][m];charname1[m],name2[m];.

    2022年6月8日
    43
  • unix命令大全详解-完整版_command方式:

    unix命令大全详解-完整版_command方式:UNIX命令大全详解-完整版command方式:任何输入都会作为编辑命令,而不会出现在屏幕上,若输入错误则有“岬”的声音;任何输入都引起立即反映insert方式:任何输入的数据都置于编辑寄存器。在command方式下输入(I,a,A等),可进入in

    2022年5月6日
    45
  • activex控件无法安装解决方法

    activex控件无法安装解决方法2015-01-06有人的电脑ie上了11的版本。结果怎么也安装不了一些activex的控件。总是被阻止。改了安全也不会好用的。因为微软IE博客介绍:“由于日益严峻的恶意网络、不断增长的恶意网页数量,因此用户非常需要确保IE浏览器ActiveX控件及时升级至最新版,从而免受各类安全漏洞攻击。”本项“Out-Of-DateActiveX(过时ActiveX控件)”拦截功能,适用于Win7SP…

    2022年5月15日
    55
  • Linux查看IP地址命令

    Linux查看IP地址命令Linux 查看公有 运营商 IP 地址 下面两个命令都可以查 Linux 查看私有 IP 地址 可以使用以下四个命令中的任一一个使用命名 nmcli pdeviceshow 可以查看设备详细信息

    2025年11月9日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号