1. 程式人生 > >string轉換成int的演算法

string轉換成int的演算法

public static int stringToInt(String input) {
		if (!isValidString(input)) {
			throw new IllegalArgumentException("input is not a number string!!");

		}
		boolean isNegative = isNegative(input);
		if (isNegative) {
			input = input.substring(1);
		}

		char[] inputArray = input.toCharArray();// 321 ,-89
		int total = 0;
		int step = 1;
		for (int i = inputArray.length - 1; i >= 0; i--) {
			total += (inputArray[i] - '0') * step;
			step *= 10;

		}

		return isNegative ? 0 - total : total;

	}

相關推薦

string轉換int演算法

public static int stringToInt(String input) { if (!isValidString(input)) { throw new IllegalArgumentException("input is not a number

String 轉換 int, long, double工具類

public class NumberUtil { private static String[] hanArr = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}; private static Str

string轉換int的幾種方式

寫在前面: 遇到了多次oj的題目需要將string轉換為int或者將int轉換為string 每次遇到都是現查,一直沒有好好的總結導致總是忘記。 現在做個總結。 首先是今天遇到的方式: 採用string標頭檔案裡定義的string型別轉換的函式 stoi

JAVA關於二維數組裡面String型別轉換int型別用於計算問題

題目的意思是這樣,要求定義一個二維陣列,並儲存學生姓名和成績,以及求成績的綜合和最大值。。     我遇到的問題的是:如何將原本定義的字串的陣列儲存的成績,轉換成int型別二維陣列,並用於求和,最終效果如下圖: int a = Integer.parseInt(a

如何將字串 String 轉換整數 int?

  A. 有兩個方法:   1). int i = Integer.parseInt([String]); 或   i = Integer.parseInt([String],[int radix]);   2). int i = Integer.valueOf(my_st

String型別轉換int型,然後再把int轉換String

public void getChanger(String str){  int n=Integer.parseInt(str);  System.out.println("轉換成int型後:"+n);  String str1=Integer.toString(n);  S

String字串轉換int[]陣列

String s = "485729304";int[] a = new int[s.length()];for(int i = 0; i < s.length(); i++){//先由字串轉換成char,再轉換成String,然後Integera[i] = Inte

演算法---String轉換Int型別(非庫函式)

public static int ConvertToInt(string num) { char[] ch = num.ToCharArray(); int i = 0, value = 0;

演算法】【將字串轉換int

之前還使用過遞迴來計算,不過現在已經記不起來了 #include <stdio.h> #include <error.h> #include <math.h> #define ERROR -22 #define DEBUG 0

[C#]"23.00"可以轉換int

int www www. string類型 public decimal 格式 word 方案 在C# 中想要將String類型轉換成int時有以下幾種方法: int.TryParse; Convert.Toint32; (int)string; 但是, 使用

sql語句 中把varchar類型轉換int

order rom sel asc col pre cas tab clas // sql語句 中把varchar類型轉換成int select * from tableName order by cast(result as SIGNED INTEGER) asc;

C++ 將string轉換char*字符串

AR string轉換 字符 str 兩種 c++ 情況 adc 需要 我們經常會使用C和C++的混合編程,在某些情況下,需要將C++的string,轉換成char* 的字符串。下面說兩種可行的方法,作為總結。 1. data(); 如: string str="abc";

java中char型別轉換int型別

PlanA: char ch = '9'; if (Character.isDigit(ch)){ // 判斷是否是數字 int num = Integer.parseInt(String.valueOf(ch)); System.out.println(num); }

String轉換整型,c_str()

程式碼 int main() { string s1 = “v”; string s2 = “vt”; int a = *s1.c_str(); int b = *s2.c_str(); cout << a << ” ” << b <&l

C# string轉換DateTime?(字串轉換可空日期型別)

最近專案中遇到以前一直困擾的問題,就是如何將string轉換成DateTime?這種可空日期型別。以前總是通過編寫一堆邏輯程式碼來進行轉換,但是寫這些程式碼感覺非常繁瑣。後在網上瀏覽相關資料,使用NullableConverter類就可以輕鬆的進行轉換。 以下是測試

mysql中int型的數字怎麼轉換字串 以及字串轉換int

MySQL 數字型別轉換函式(concat/cast)。 1、將Int 轉為varchar經常用 concat函式,比如concat(8,’0′) 得到字串 ’80′。 2、將varchar 轉為Int 用 cast(a as signed) a為varchar型別的字串

Mysql中的varchar型別轉換int型別

Mysql中的varchar型別轉換成int型別 1.實戰案例 1.1 student表結構 mysql> desc student; +----------+-------------+----

C# string轉換datetime

將字串轉換為日期string value= "2009-11-25"DateTime dt= Convert.ToDateTime(value);日期轉換為字串(2009,11,25 )string s = dt.ToString("yyyy,mm,dd");

java如何將char型別的數字轉換int型的數字

昨天做筆試提的過程中遇到一個問題: 如何把 char ‘3’ 轉為 int 3, 大家應該知道,不能直接轉化,那樣得到是‘3’的Ascii. 如下面:public class CharToIntConverter {          public static void m

C#之將從textbox獲取的值從string轉換int

測試程式碼:方法一try            {                int count1 = int.Parse(textBox2.Text); //string型別轉換int型別            }            catch (Exception