GTK+中文社区(gtk.awaysoft.com)

 找回密码
 马上加入

QQ登录

只需一步,快速开始

查看: 4395|回复: 3

GString基本操作一

[复制链接]

该用户从未签到

发表于 2010-8-30 17:27:41 | 显示全部楼层 |阅读模式
GString是Gtk+中非常实用的一个字符串操作结构,可以使对C语言中字符串操作指针不熟悉的程序员,快速的操作字符串,而且不出错,下面的代码演示了GString操作Append(追加),Insert(指定位置插入),OverWrite(指定位置复写),Erase(指定位置删除)和Equal(比较)
作者:AlimysoYang
  1. #include <gtk/gtk.h>
  2. GtkWidget *wnd;
  3. GtkWidget *vbox,*hbox,*et1,*et2,*et3,*btn,*btninsert,*btnoverwrite,*btnerase,*btnequal;
  4. void ui_init()
  5. {
  6.     wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  7.     gtk_window_set_title(GTK_WINDOW(wnd),"GString about prepend");
  8.     gtk_window_set_position(GTK_WINDOW(wnd),GTK_WIN_POS_CENTER);
  9.    
  10.     vbox = gtk_vbox_new(TRUE,2);
  11.     gtk_container_add(GTK_CONTAINER(wnd),vbox);
  12.    
  13.     et1 = gtk_entry_new();
  14.     et2 = gtk_entry_new();
  15.     et3 = gtk_entry_new();
  16.     gtk_entry_set_editable(GTK_ENTRY(et3),FALSE);
  17.    
  18.     hbox = gtk_hbox_new(TRUE,2);
  19.     btn = gtk_button_new_with_label("GString prepend");
  20.     btninsert = gtk_button_new_with_label("GString insert");
  21.     btnoverwrite = gtk_button_new_with_label("GString overwrite");
  22.     btnerase = gtk_button_new_with_label("GString erase");
  23.     btnequal = gtk_button_new_with_label("GString equal");
  24.    
  25.     gtk_box_pack_start_defaults(GTK_BOX(vbox),et1);
  26.     gtk_box_pack_start_defaults(GTK_BOX(vbox),et2);
  27.     gtk_box_pack_start_defaults(GTK_BOX(vbox),et3);
  28.     gtk_box_pack_start_defaults(GTK_BOX(vbox),hbox);
  29.    
  30.     gtk_box_pack_start_defaults(GTK_BOX(hbox),btn);
  31.     gtk_box_pack_start_defaults(GTK_BOX(hbox),btninsert);
  32.     gtk_box_pack_start_defaults(GTK_BOX(hbox),btnoverwrite);
  33.     gtk_box_pack_start_defaults(GTK_BOX(hbox),btnerase);
  34.     gtk_box_pack_start_defaults(GTK_BOX(hbox),btnequal);
  35.    
  36.     gtk_widget_show_all(wnd);
  37. }
  38. static void btn_clicked(GtkButton *button,gpointer user_data)
  39. {
  40.     const gchar *str1 = gtk_entry_get_text(GTK_ENTRY(et1));
  41.     const gchar *str2 = gtk_entry_get_text(GTK_ENTRY(et2));
  42.     GString *gstr = g_string_new(str1);
  43.     g_string_prepend(gstr,str2);
  44.     g_string_up(gstr);
  45.     gtk_entry_set_text(GTK_ENTRY(et3),gstr->str);
  46.     g_string_free(gstr,TRUE);
  47. }
  48. static void btninsert_clicked(GtkButton *button,gpointer user_data)
  49. {
  50.     const gchar *str1 = gtk_entry_get_text(GTK_ENTRY(et1));
  51.     const gchar *str2 = gtk_entry_get_text(GTK_ENTRY(et2));
  52.     GString *gstr = g_string_new(str1);
  53.     int len = gstr->len;
  54.     int pos = len - 2;
  55.     if (pos<0)
  56.     pos = 0;
  57.     g_string_insert(gstr,pos,str2);
  58.     g_string_down(gstr);
  59.     gtk_entry_set_text(GTK_ENTRY(et3),gstr->str);
  60.     g_string_free(gstr,TRUE);
  61. }
  62. static void btnoverwrite_clicked(GtkButton *button,gpointer user_data)
  63. {
  64.     const gchar *str1 = gtk_entry_get_text(GTK_ENTRY(et1));
  65.     const gchar *str2 = gtk_entry_get_text(GTK_ENTRY(et2));
  66.     GString *gstr = g_string_new(str1);
  67.     int len = gstr->len;
  68.     int pos = len - 2;
  69.     if (pos<0)
  70.     pos = 0;
  71.     g_string_overwrite(gstr,pos,str2);
  72.     gtk_entry_set_text(GTK_ENTRY(et3),gstr->str);
  73.     g_string_free(gstr,TRUE);
  74. }
  75. static void btnerase_clicked(GtkButton *button,gpointer user_data)
  76. {
  77.     const gchar *str = gtk_entry_get_text(GTK_ENTRY(et1));
  78.     GString *gstr = g_string_new(str);
  79.     int len = gstr->len;
  80.     gssize pos = len - 4;
  81.     g_string_erase(gstr,pos,2);
  82.     gtk_entry_set_text(GTK_ENTRY(et3),gstr->str);
  83.     g_string_free(gstr,TRUE);
  84. }
  85. static void btnequal_clicked(GtkButton *button,gpointer user_data)
  86. {
  87.     const gchar *str1 = gtk_entry_get_text(GTK_ENTRY(et1));
  88.     const gchar *str2 = gtk_entry_get_text(GTK_ENTRY(et2));
  89.     GString *gstr1,*gstr2;
  90.     gstr1 = g_string_new(str1);
  91.     gstr2 = g_string_new(str2);
  92.     if (g_string_equal(gstr1,gstr2))
  93.     g_printf("the same strings\n");
  94.     else
  95.     g_printf("the different strings\n");
  96.     g_string_free(gstr1,TRUE);
  97.     g_string_free(gstr2,TRUE);
  98. }
  99. int main(int argc,char *argv[])
  100. {
  101.     gtk_init(&argc,&argv);
  102.    
  103.     ui_init();
  104.    
  105.     g_signal_connect(G_OBJECT(wnd),"destroy",G_CALLBACK(gtk_main_quit),NULL);
  106.     g_signal_connect(G_OBJECT(btn),"clicked",G_CALLBACK(btn_clicked),NULL);
  107.     g_signal_connect(G_OBJECT(btninsert),"clicked",G_CALLBACK(btninsert_clicked),NULL);
  108.     g_signal_connect(G_OBJECT(btnoverwrite),"clicked",G_CALLBACK(btnoverwrite_clicked),NULL);
  109.     g_signal_connect(G_OBJECT(btnerase),"clicked",G_CALLBACK(btnerase_clicked),NULL);
  110.     g_signal_connect(G_OBJECT(btnequal),"clicked",G_CALLBACK(btnequal_clicked),NULL);
  111.     gtk_main();
  112.     return 0;
  113. }
复制代码

评分

参与人数 1 +11 收起 理由
Tom + 11

查看全部评分

  • TA的每日心情
    奋斗
    2021-11-19 13:15
  • 签到天数: 20 天

    连续签到: 1 天

    [LV.4]偶尔看看III

    发表于 2010-8-30 20:04:15 | 显示全部楼层
    GString确实很方便,不用自己去考虑空间等问题了,学习了。。。

    该用户从未签到

    发表于 2010-10-30 07:07:57 | 显示全部楼层
    先学习,再收藏。哈哈~~谢谢LZ。

    该用户从未签到

    发表于 2010-11-10 03:12:17 | 显示全部楼层
    *滑块验证:
    您需要登录后才可以回帖 登录 | 马上加入

    本版积分规则

    申请友链|Archiver|小黑屋|手机版|GTK+中文社区 ( 粤ICP备13080851号 )

    我要啦免费统计

    GMT+8, 2024-12-22 17:29 , Processed in 0.038161 second(s), 9 queries , Redis On.

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表