КАТЕГОРИИ: Архитектура-(3434)Астрономия-(809)Биология-(7483)Биотехнологии-(1457)Военное дело-(14632)Высокие технологии-(1363)География-(913)Геология-(1438)Государство-(451)Демография-(1065)Дом-(47672)Журналистика и СМИ-(912)Изобретательство-(14524)Иностранные языки-(4268)Информатика-(17799)Искусство-(1338)История-(13644)Компьютеры-(11121)Косметика-(55)Кулинария-(373)Культура-(8427)Лингвистика-(374)Литература-(1642)Маркетинг-(23702)Математика-(16968)Машиностроение-(1700)Медицина-(12668)Менеджмент-(24684)Механика-(15423)Науковедение-(506)Образование-(11852)Охрана труда-(3308)Педагогика-(5571)Полиграфия-(1312)Политика-(7869)Право-(5454)Приборостроение-(1369)Программирование-(2801)Производство-(97182)Промышленность-(8706)Психология-(18388)Религия-(3217)Связь-(10668)Сельское хозяйство-(299)Социология-(6455)Спорт-(42831)Строительство-(4793)Торговля-(5050)Транспорт-(2929)Туризм-(1568)Физика-(3942)Философия-(17015)Финансы-(26596)Химия-(22929)Экология-(12095)Экономика-(9961)Электроника-(8441)Электротехника-(4623)Энергетика-(12629)Юриспруденция-(1492)Ядерная техника-(1748) |
Standard functions applying to AnsiString-type arguments
The list of the most important functions is following: + – concatenation. Operator S1=S2+S3; says that string from S3 must be appended to those from S2 and the result saved in S1. ==,!=, <, <=, >, >= – logical operations. Expression S1==S2 returns bool-type value; as well as expressions with other 5 operations. Now let S be an AnsiString-type variable. Any following C++-Builder function F applying to S must be called in the form: S.F(<additional parameters (if any) of F>); (You will understand the motives of choosing such a form after you learn the notion of class of C++-Builder.) S.c_str() – the result of this function is a zero-ended array contains a copy of string S. So if you have AnsiString S = ”Honey”; then it will be true that S.c_str()[0] == ’H’ S.Length() – returns an int number – the current length of S. S.IsEmpty() – returns a bool value (true if S is empty, false otherwise.) S.Pos(sub_S) – returns the index of a first occurrence of a substring sub_S in the string S. If there are no occurrences of sub_S, it returns 0. Parameter can be a character, a constant string, or an AnsiString variable. So if S contains the string ”twins”, the value of S.Pos(”win”) equals to 2. S.SubString(iBeg, sLen) – returns the substring of S, which begins at index iBeg and has sLen characters in length. So if S contains the string ”twins”, the value of S.SubString(2,3) equals to ”win”. S.Delete(iBeg, sLen) – deletes sLen characters from starting from index iBeg and returns the result of deletion. So if S contains the string ”twins”, the value of S.Delete(2,3) equals to ”ts”. S.Insert(S1, iIns) – inserts the string S1 into the string S in the position marked by index iIns. So if S contains the string ”twins”, and S1 contains ”o_tw”, the value of S.Insert(S1,3) equals to ”two_twins”. Just the same result will be obtained, if S1 contains ”two_” and we use function calls S.Insert(S1, 1) or S1.Insert(S, S1.Length()+1). If you look the “Help” to the AnsiString-type you can see many other useful functions which help to manipulate this kind of data. Let us now compose some programs dealing with AnsiStrings. Example 1. Write a function that erases all ending blanks from a given AnsiString. One of possible solutions is: AnsiString DelEndBlanks(AnsiString S) { const char Blank=' '; int Len=S.Length(); if(Len==0)return S; while((Len>0)&& (S[Len]==Blank)) {S.Delete(Len,1); Len--; } return S; }
You see from this example that it is possible to call the AnsiString-function Delete in a mode normally used for functions having void returned values. But in fact Delete returns AnsiString value (more precisely, a pointer to an AnsiString value); and the above operator S.Delete(Len,1) is equivalent to S=S.Delete(Len,1). You will understand the cause of this equivalence after you will learn the notion of class of C++-Builder.
Example 2. Write a function that erases all multiple blanks from a given AnsiString (it means that any sequence of blanks must be substituted for a single blank). One of possible solutions is: AnsiString DelMltplBlanks(AnsiString S) { const AnsiString twoBlanks=" "; int Len=S.Length(); if(Len==0)return S; int j=S.Pos(twoBlanks); while((Len>0)&& (j>0)) {S.Delete(j,1); Len--; j=S.Pos(twoBlanks); } return S; } Example 3. Write a function that returns the first word in a given AnsiString. It is assumed that blank symbol is the only possible delimiter of words in the string. One of possible solutions is: AnsiString Get1stWordIn(AnsiString S) { const char Blank=' '; int Len=S.Length(); if(Len==0)return S;
S=DelMltplBlanks(S); S=DelEndBlanks(S); S=DelBegBlanks(S); // write this function yourself Len=S.Length(); if(Len==0)return S;
int j=S.Pos(Blank); if(j==0)return S;
return S.SubString(1,j-1); }
Дата добавления: 2014-01-11; Просмотров: 354; Нарушение авторских прав?; Мы поможем в написании вашей работы! Нам важно ваше мнение! Был ли полезен опубликованный материал? Да | Нет |