- 浏览: 405979 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
yiming163:
thank you!
eCATT, LSMW in detail -
泡泡蓝:
把WITH INCLUDES.改成 WITH ANALYSI ...
User Exits,Customer Exits,BAdI and BTE -
泡泡蓝:
王弈争 写道泡泡蓝 写道SCAN ABAP-SOURCE SO ...
User Exits,Customer Exits,BAdI and BTE -
王弈争:
泡泡蓝 写道SCAN ABAP-SOURCE SOURCETA ...
User Exits,Customer Exits,BAdI and BTE -
jgtang82:
泡泡,我没遇到你所说的问题呀
User Exits,Customer Exits,BAdI and BTE
Chinese Version: http://blog.csdn.net/CompassButton/archive/2009/09/23/4583951.aspx
Sourse: http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/15889
Character variables
Variables of type c have a specified length declared in the TYPES or DATA declaration section. The length can not change, and there is no length indicator nor an end-of-char like \0 in c. Blanks to the right of the content are always ignored by the system, a char variable containing only blanks is treated as empty/initial. Clearing a char variable will fill it completly with blanks.
DATA: cvar TYPE c LENGTH 20,
len TYPE i.
cvar = ' '.
len = STRLEN( cvar ).
WRITE:/ 'Charlen: ', len.
A blank is assigned to the variables, but still the length is zero. The compiler cant differ between internally used blanks and blanks assigned by the programmer.
There is one exception to this rule: the separated by clause in the concatenate command. Here a ' ' will be a blank and not an empty character sequence.
Variables of type c are perfect to store a small amount of data. A length of 30 characters will fit 99% of storing a last name. But what are you doing if one of your customer will be Hadschi Halef Omar Ben Hadschi Abul Abbas Ibn Hadschi Dawuhd al Gossarah? Expanding the length to fit this name will waste a lot of memory. The answer is: use strings.
String variables
Variables of type string consists of two parts: a header with administration data and the string data itself. In the administration data a pointer to the content, and a length indicator. A string can hold 2<super>31</super>-1 characters, but this is rather a theoretical value, more often you will be limited by the profil parameter ztta/max_memreq_MB or by the amount of concurrent running processes.
As strings do have a length indicator they can contain blanks even at the end of the content:
DATA: svar TYPE string,
len TYPE i.
svar = ' '.
len = STRLEN( svar ).
WRITE:/ 'Stringlen: ', len.
If you run this little piece of code, the result will be .... zero! But don't have strings the ability of storing blanks? Shouldn't the result be one? The trap we felt into was using a character literal: ' '. A character literal has the same restrictions as character variables: blanks at the end are ignored, an initial value is treated as empty. So at least we assigned an empty character value to a string which will be of course empty, too.
Using a string literal will avoid this. The difference between a character literal and a string literal are the enclosing tags: apostrophes for a character literal and back quotes for string literals. Using a string literal we will get the expected result:
DATA: svar TYPE string,
len TYPE i.
svar = ` `.
len = STRLEN( svar ).
WRITE:/ 'Stringlen: ', len.
Note: the keyword SPACE is a predefined character literal with length 1. Consequently, assigning SPACE to a string variable will not assign a blank! Using string literals will help us in cases where we really need a blank. Imagine you want to replace all semicolons in a variable with blanks:
cvar = 'A;B;C;D;E'.
REPLACE ALL OCCURRENCES OF ';' IN cvar WITH ' '. " (1)
WRITE:/ cvar.
cvar = 'A;B;C;D;E'.
REPLACE ALL OCCURRENCES OF ';' IN cvar WITH space. " (2)
WRITE:/ cvar.
cvar = 'A;B;C;D;E'.
REPLACE ALL OCCURRENCES OF ';' IN cvar WITH ` ` . " (3)
WRITE:/ cvar.
svar = `A;B;C;D;E`.
REPLACE ALL OCCURRENCES OF ';' IN svar WITH ` `. " (4)
WRITE:/ svar.
Now we can understand why (1) and (2) will not give the desired result, the character literal is initial/empty so the result will be ABCDE without any blank in between. (3) will not work either, the string literal will be converted to character cause the target is a character variable. Only (4) will give us the result A B C D E.
!! Unfortunately, i had an error in my test report, so (3) will work giving the desired result A B C D E !!
Mixing with other types
Be careful when mixing character and string variables with other data types:
DATA: cvar TYPE c LENGTH 20,
svar TYPE string,
pvar TYPE p LENGTH 5,
l TYPE i.pvar = 5.cvar = pvar.
l = STRLEN( cvar ).
WRITE:/ 'Content: |', cvar, '| (', l, ')'.svar = pvar.
l = STRLEN( svar ).
WRITE:/ 'Content: |', svar, '| (', l, ')'.
Using the character variable the 5 is right justified with a blank at its end. The blank at the end is the sign, and as trailing blanks are not part of the content the length is 19.
Using the string variable the 5 is left justified with a trailing blank which is the sign. As blanks at the end in strings are part of the content the length is 2.
So be careful when assigning types to chars or strings, check if the result fullfill your needs.
Disadvantages using strings
1. Performance
Performance is not really an issue, even when assigning character literals to strings the needed type conversion is beneath notice.
2. Substrings
Using character variables you can assign new values to a substring:
cvar = 'Hello World'.
cvar+5(1) = ','.
write:/ cvar.
When you try this with a string you will get a compiler error saying that you cannot use offest und length specification. Instead, you need to use the REPLACE command:
svar = 'Hello World'.
" svar+5(1) = ','. <= compiler error
REPLACE SECTION OFFSET 5 LENGTH 1 OF svar WITH ','.
WRITE:/ svar.
So, you have to write a little bit more code.
Translations
If you need to translate your report to different languages you can just add a nummer in brackets after the literal 'Error Message'(001) which is more readable as text-001. This is not working with string literals.
Helping classes
Check
CL_ABAP_CHAR_UTILITIES and
CL_ABAP_STRING_UTILITIES to find some useful methods.
发表评论
-
Sap Number Range Object (SNRO)
2010-12-21 16:11 1828Brief Example http://www.sapte ... -
SAP R/3 Security Tables
2010-12-09 15:27 1616Below the list of SAP R/3 Sec ... -
Class def & impl, Inherit & overwriting, interface & using it, event & handler
2010-12-08 10:05 1148Below codes is a simply and rou ... -
IDOC_INPUT_ORDERS with error msg RV45A-VBAP_SELKZ (2) is not input field
2010-11-08 14:51 2403[Issue] Hi I am creating a mul ... -
SAP ALV Trees
2010-11-05 14:07 2151BCALV_GRID_DND_TREE ALV Grid: D ... -
Creating a SAP shortcut for any transaction and sending it by mail
2010-11-02 11:12 2002Refer to SAP wiki: http://wiki. ... -
Note 573128 - Debugging programs in the background
2010-11-01 20:13 1176Note 573128 - Debugging program ... -
Function Module related on Date calculations (ZT)
2010-07-20 17:00 1690http://wiki.sdn.sap.com/wiki/di ... -
SAP SDN Interesting Topics
2010-06-29 09:48 984Web Dynpro ABAP Performance Too ... -
SE16N &SAP_EDIT, ICON at Selection Screen
2010-06-12 17:35 1732摘录From blog of 翱翔云天 1. SE16N & ... -
Debug background processes, update/system code,model dialog...
2009-12-18 13:55 1197How do I debug running backgrou ... -
ECC6 ALV Dump
2009-10-21 15:20 1032[Dump] 1. Use system reserved ... -
Simple Transfermation Program
2009-08-07 23:42 1994Have you been requested to gene ... -
eCATT, LSMW in detail
2009-02-15 10:14 3451eCATT : http://sap.iteye.com/bl ... -
Trigger ABAP program using UNIX script
2009-01-09 13:27 22421. Create a batch job in SM37 f ... -
How to cancel active job, del/change schedule job
2008-12-26 09:56 2305if the job is active and you wa ... -
How to Run UNIX script from ABAP?
2008-12-19 12:52 2526Look at SM69, SM49 and Functio ... -
REUSE_ALV_GRID_DISPLAY事件子过程和cl_gui_grid类的事件对应关系
2008-12-04 18:46 3855一、SLIS定义的ALV的事件名称* Eventsslis_e ... -
DDIC info related tables
2008-12-03 09:58 1943Data elementsDD04L Data elem ... -
Popup text editor control
2008-11-25 11:48 1115DATA: BEGIN OF IT_TEXT OCCURS 0 ...
相关推荐
ABAP Program Tips v3. 适合abap初级选手学习
SAP ABAP 参考阅读库.CHM,可以参考着看看
ABAP语法详细列表.非常适合学习者. ABAP语法详细列表.非常适合学习者.
ABAP开发培训教材.pptx
sapabap采购订单报表.doc
abap程序员之路.rarabap程序员之路.rarabap程序员之路.rarabap程序员之路.rarabap程序员之路.rarabap程序员之路.rar
ABAP语法简明资料.doc
ABAP树型菜单程序.pdf
《ABAP名词中文一览.doc》 适合新手阅读的abap教程 非常好
SAP+ABAP学习路线图.docx
SAPBC400ABAP工作台简介.pptx
SAP及ABAP综合培训教案.pptx
ERP系统信息化资料:7. 第七章 控制 ABAP4 程序流.doc
ABAP PA,全称为ABAP for Personnel Administration,是SAP人力资源模块中的一个重要组成部分,用于处理企业内部的人力资源管理事务。在本压缩包文件“ABAP PA中文教材.zip”中,我们可以期待找到一系列关于如何使用...
ABAP-培训教程.ppt该文档详细且完整,值得借鉴下载使用,欢迎下载使用,有问题可以第一时间联系作者~
ABAP学习手册归类.pdf ABAP 是一种高级编程语言,主要用于 SAP 系统中开发应用程序。在本文档中,我们将对 ABAP 的基础知识和应用进行系统性地总结和归类。 一、ABAP 基础知识 1.1 Basis TSTCT 事务代码文本 ...