Show toolbar
顯示具有 String 標籤的文章。 顯示所有文章
顯示具有 String 標籤的文章。 顯示所有文章

2013年7月8日 星期一

String conversion

標題:C/C++字串轉換
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream> //ostringstream
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    //Character Sequences
    //char c[13] = "CharVariable";
    char c[13] = {'C', 'h', 'a', 'r', 'V', 'a', 'r', 'i', 'a', 'b', 'l', 'e', '\0'};

    //Pointer
    char *p = "Pointer";

    //Class
    string s = "String";

    //String to Char
    strcpy(c, s.c_str());
    cout << c << endl;
    
    //Char to String
    s.assign(c);
    cout << s << endl;

    //Pointer to Char
    strcpy(c, p);
    cout << c << endl;

    //Char to Pointer
    p = &c[0];
    cout << p << endl;

    //String to Float
    float num1 = 0.0f;
    s = "233.42";
    num1 = atof(s.c_str());
    cout << num1 << endl;

    //Float to String
    float num2 = 233.42f;
    ostringstream buffer;
    buffer << num2;
    s = buffer.str();
    cout << s << endl;
    
    return 0;
}
說明:
字元陣列、指標字串、VC字串,互相轉換。

2013年4月25日 星期四

Verify a character is in English or number

標題:使用ASCII判斷字元是英文或數字
C (main.html):
#include <stdio.h>
#include <string.h> //供strlen使用

int main(void)
{
    int i = 0;
    char str[9] = "_09azAZ?";
    for(i=0;i<strlen(str);i++) //檢查字串中的字元是否均為英文
    {
        //使用十進制ASCII碼判斷字元
        if(str[i] >= 48 && str[i] <= 57) //數字的ASCII碼落在48 ~ 57
        {
            printf("%c 是數字\n", str[i]);
        }
        else if (str[i] >= 65 && str[i] <= 90) //大寫的ASCII碼落在65 ~ 90
        {
            printf("%c 是大寫\n", str[i]);
        }
        else if (str[i] >= 97 && str[i] <= 122) //小寫的ASCII碼落在97 ~ 122
        {
            printf("%c 是小寫\n", str[i]);
        }
        else //其它非英數
        {
            printf("%c 非英數\n", str[i]);
        }
    }
    return 0;
}


說明:
使用ASCII碼十進制判斷字串中的字元是英文或數字,其中注意到字元陣列大小為8(字元數)+1(字串結束符)=9。

2012年8月17日 星期五

Batch Capital To Lower

標題:英文字母大寫轉小寫
Bacth (c2t.bat):
@Echo Off
SetLocal EnableDelayedExpansion
set var=CapitalToLower
echo !var!
for %%a in (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) do (
    set var=!var:%%a=%%a!
)
echo !var!
PAUSE
說明:
使用Batch語法大寫字母轉小寫字母。

2012年7月10日 星期二

C++ String Reverse

標題:C++連續字串反轉
C++ (main.cpp):
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main(void) {
    char str[99];
    int i;
    while(cin.getline(str, 99, '\n')) {
        for (i=strlen(str)-1;i>=0;i--) {
            cout << str[i];
        }
        cout << endl;
    }
    return 0;
}

範例結果:


說明:
基礎連續字串反轉範例。

2011年11月16日 星期三

Python String

標題:Python3常用字串語法
Python (main.py):
# -*- coding: utf-8 -*-
english = "-*-QQBoxy Say Hello CodeBoxy-*-"
print(english[:3]) #由最前面印到第3個字
print(english[3:-3]) #由第3個字印到倒數第3個字
print(english[-3:]) #由倒數第3個字印到最後
print(english.strip("-*")) #清除字串前後字元
print(english.strip("-*").lower()) #轉成小寫
print(english.strip("-*").upper()) #轉成大寫
print(english.strip("-*").replace("Boxy", "Box", 1)) #取代1次字串
print(english.strip("-*").split(" ")) #陣列切割
print(len(english)) #字串長度
print(english.find("Boxy", 3, 27)) #位置3到位置27字串第一次出現的位置
print(english.count("Boxy", 0, 27)) #位置0到位置27字串出現的次數
範例結果: 說明:
Python3常用字串語法整理。