2010年3月5日 星期五

轉貼 google app engine ado store 長字串

在 google app engine 中, string 的儲存 有長度限制為500 , 也就是超過500 的長度以後的字串就會被忽略

詳細的說明在google 有說明

所以要存長字串的話,可以使用 Text 的內容來儲存
不過宣告前要加一段code 如下
@Persistent(serialized="true", defaultFetchGroup="true")
private Text text;

(轉貼 自 com.google.appengine.api.datastore.Text 不能保存 )


此外要讀成 string 的時候

是用
text.getValue() 這個function

text.toString() 只有 列出前70 個字元而已...



注意: Text 型別不能用來查詢

2010年2月25日 星期四

轉貼 C# 與 unicode 互換

在 c# 將 字串 轉換為 unicode 字串(\u....\u....)




/**
* @name StringToUnicode
*
* String To Unicode
*
*
* @param srcText [in] - the table name
*
*
*/
private string StringToUnicode(string srcText)
{
string dst = "";
char[] src = srcText.ToCharArray();
for (int i = 0; i < src.Length; i++)
{
byte[] bytes = Encoding.Unicode.GetBytes(src[i].ToString());
string str = @"\u" + bytes[1].ToString("X2") + bytes[0].ToString("X2");
dst += str;
}
return dst;
}




將 unicode 字串轉回 字串



/**
* @name UnicodeToString
*
* Unicode To String
*
*
* @param srcText [in] - the table name
*
*
*/
private string UnicodeToString(string srcText)
{
string dst = "";
string src = srcText;
int len = srcText.Length / 6;

for (int i = 0; i <= len - 1; i++)
{
string str = "";
str = src.Substring(0, 6).Substring(2);
src = src.Substring(6);
byte[] bytes = new byte[2];
bytes[1] = byte.Parse(int.Parse(str.Substring(0,2),NumberStyles.HexNumber).ToString());
bytes[0] = byte.Parse(int.Parse(str.Substring(2, 2),NumberStyles.HexNumber).ToString());
dst += Encoding.Unicode.GetString(bytes);
}
return dst;
}



轉貼自

C#字串与Unicode互相转换方法

2010年2月22日 星期一

String split in c#

傳送資料時,若資料是以string array 的方式紀錄的話,若要將這些資料傳送應該有很多種方法(待查證), 這裡先用一個簡單的方式 就是自行加入分割的字串, 接收端收到之後在用(string.split )作資料的分割

string split 的用法可以參考MSDN

一般使用字元分割到是很容易理解就不加以敘述, 這裡說明一下用 字串分割的範例

假設 自訂的分割字串符號 為 "_CCC_"

注意 StringSplitOptions 下有兩種 方法
1. none : 預設
2. RemoveEmptyEntries : 移除空白的字串在新字串陣列中


以下示範裡的 code




/// the string not split
string rawString="this_CCC_is_CCC_test";

/// Declare the split string
string[] splitString = new string[]{"_CCC_"};

/// The Goal Array
string[] theGaolaArray = rawString.Split(splitString, StringSplitOptions.RemoveEmptyEntries);



2010年2月5日 星期五

C# DateTime 格式應用

常常會需要一些隨機產生的變數,可是又不希望這些變數重複,應此第一個想到的就是使用系統的datetime 來轉換
以下是我用過得一些紀錄



1. 取得現在的時間
Using System.DateTime 這個 structure

DateTime nowDateTime = DateTime.Now;

2. 取得現在的UTC時間

DateTime nowDateTime = DateTime.UtcNow;

3. 日期格式轉字串

string sDateTime = nowDateTime.ToString("yyyyMMddHHmmss");

這個例子是將現在時間轉成一個字串,
假設現在是 2010/2/5,10:00:00 就會轉換成
20100205100000
其中像 yyyy, MM 那些符號代表的意義,可以參考 MSDN 的說明



4. 字串轉日期

常常會有機會將日期格式的資料型態轉乘文字,如利用 toString()這個函式,這時候在程式運算中有必要在將她轉成日期的格式。
就可以利用 DateTime.Parse()
或 Convert.ToDateTime()
這兩個函式


5. 建立日期函式

函式

DateTime dt = new DateTime(year, month, day, hour, minute, second, millisecond);
year, month, day, hour, minute, second, millisecond 都是 int 的格式,
不過要注意每個的變數範圍
或

DateTime dt = new DateTime(year, month, day);

Google App Engine 索引設定

google app engine(GAE) 提供了線上儲存資料的功能,不過感覺上是將每個物件紀錄在網路上,

所以若要使用 像 sql 的 Java Data Objects (jdo) 就必須建立 索引(index)
當然, python 也有一樣的方法,不過這裡介紹以java 為主

基本上,利用eclipse 的外掛開發是比較省力的,所以我的介紹還是以 eclipse 搭配 GAE 的外掛環境來說明

建立索引的方法可以利用利用GAE 自動產生或手動輸入

自動產生的話,可以先在 local 端 先吧應用程式跑一遍,就會產生了,不過若是直接傳到網路上的時候就不會自動產生了... 這點我吃了很多苦頭,所以才會有這一篇文章作紀錄

自己輸入的話
找到datastore-indexes.xml 修改就可以了

詳細介紹如下
http://code.google.com/intl/en/appengine/docs/java/config/indexconfig.html