Студопедия

КАТЕГОРИИ:


Архитектура-(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)

Компиляция кода. Поиск строки в массиве строк




Пример

Поиск строки в массиве строк

Robust Programming

Compiling the Code

Example

Компиляция кода

Пример

Создание многострочных строковых литералов

Compiling the Code

Example

string myString1 = "This is the first line of my string.\n" + "This is the second line of my string.\n" + "This is the third line of the string.\n"; string myString2 = @"This is the first line of my string. This is the second line of my string. This is the third line of the string.";

Copy the code and paste it into the Main method of a console application.


В следующем примере представлено два способа создания многострочных строковых литералов.

string myString1 = "This is the first line of my string.\n" + "This is the second line of my string.\n" + "This is the third line of the string.\n"; string myString2 = @"This is the first line of my string. This is the second line of my string. This is the third line of the string.";

Скопируйте код и вставьте его в метод Main консольного приложения.


How to: Search for a String in an Array of Strings

This example calls the IndexOf method on an array of strings to report the string number and index of the first occurrence of a substring.

 

string[] strArray = {"ABCDEFG", "HIJKLMNOP"}; string findThisString = "JKL"; int strNumber; int strIndex = 0; for (strNumber = 0; strNumber < strArray.Length; strNumber++) { strIndex = strArray[strNumber].IndexOf(findThisString); if (strIndex >= 0) break; } System.Console.WriteLine("String number: {0}\nString index: {1}", strNumber, strIndex);

 

Copy the code and paste it into the Main method of a console application.

 

 

The IndexOf method reports the location of the first character of the first occurrence of the substring. The index is 0-based, which means the first character of a string has an index of 0.

If IndexOf does not find the substring, it returns -1.

The IndexOf method is case-sensitive and uses the current culture.

If you want more control over possible exceptions, enclose the string search in a try-catch statement.


В этом примере вызывается метод IndexOf для массива строк, который сообщает номер строки и индекс первого вхождения подстроки.

string[] strArray = {"ABCDEFG", "HIJKLMNOP"}; string findThisString = "JKL"; int strNumber; int strIndex = 0; for (strNumber = 0; strNumber < strArray.Length; strNumber++) { strIndex = strArray[strNumber].IndexOf(findThisString); if (strIndex >= 0) break; } System.Console.WriteLine("String number: {0}\nString index: {1}", strNumber, strIndex);

Скопируйте код и вставьте его в метод Main консольного приложения.




Поделиться с друзьями:


Дата добавления: 2014-12-27; Просмотров: 316; Нарушение авторских прав?; Мы поможем в написании вашей работы!


Нам важно ваше мнение! Был ли полезен опубликованный материал? Да | Нет



studopedia.su - Студопедия (2013 - 2024) год. Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав! Последнее добавление




Генерация страницы за: 0.008 сек.