将字符串转化成XX格式

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LogPrint;

public class CommonHelper
{
private static readonly string[] null_Arrar = new string[0];

//以分号分割把字符串转化为vector2
public static Vector2 StrToVector2(string str)
{
    if (string.IsNullOrEmpty(str)) return Vector2.zero;
    Vector2 value = Vector2.zero;
    string[] strArray = str.Split(';');
    if (strArray.Length < 2)
    {
        LogSystem.LogError(str + " string to Vector2 failed");
        return value;
    }
    float x = Convert.ToSingle(strArray[0]);
    float y = Convert.ToSingle(strArray[1]);
    value = new Vector2(x, y);

    return value;
}

//以分号分割把字符串转化为vector3
public static Vector3 StrToVector3(string str)
{
    if (string.IsNullOrEmpty(str))
        return Vector3.zero;

    Vector3 value = Vector3.zero;
    string[] strArray = str.Split(';');
    if (strArray.Length < 3)
    {
        LogSystem.LogError(str + " string to Vector3 failed");
        return value;
    }
    if (strArray[0] != "" && strArray[1] != "" && strArray[2] != "")
    {
        float x = Convert.ToSingle(strArray[0]);
        float y = Convert.ToSingle(strArray[1]);
        float z = Convert.ToSingle(strArray[2]);
        value = new Vector3(x, y, z);
    }


    return value;
}

/// <summary>
/// 字符串转换数组;
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string[] Str2StringArray(string str, char splitMark = ';')
{
    if (string.IsNullOrEmpty(str))
    {
        return null_Arrar;
    }
    string[] strArray = str.Split(splitMark);
    return strArray;
}

/// <summary>
/// 字符串转成float;
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static float Str2Float(string str)
{
    float refloat = 0;
    try
    {
        refloat = string.IsNullOrEmpty(str) == true ? 0 : Convert.ToSingle(str);
    }
    catch (Exception e)
    {

        LogSystem.Log(str + "::" + e.Message);
    }
    return refloat;
}

public static int Str2Int(string str)
{
    return string.IsNullOrEmpty(str) == true ? 0 : Convert.ToInt32(str);
}

//将数字转换成2位数 3 -> 03
public static string DoubleDigit(int score)
{
    if (score > 9)
    {
        return score.ToString();
    }
    else if (score < 0)
    {
        //只能显示正整数,负数不要显示
        return "9527";
    }
    else
    {
        return "0" + score;
    }
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值