# Строки

В пакет java.lang встроен класс, инкапсулирующий структуру данных, соответствующую строке. Этот класс, называемый **String**, не что иное, как объектное представление неизменяемого символьного массива. В этом классе есть методы, которые позволяют сравнивать строки, осуществлять в них поиск и извлекать определенные символы и подстроки. Класс **StringBuffer** используется тогда, когда строку после создания требу­ется изменять.

```
String myString = "This is my string. There are many like it but this one is mine";

String anotherString = new String();
```

## Полезные методы класса String

**length**

```
public int length()
```

**isEmpty**

```
public boolean isEmpty()
```

**charAt**

```
public char charAt(int index)
```

**getChars**

```
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
```

**equals**

```
public boolean equals(Object anObject)
```

**equalsIgnoreCase**

```
public boolean equalsIgnoreCase(String anotherString)
```

**startsWith**

```
public boolean startsWith(String prefix, int toffset)

public boolean startsWith(String prefix)
```

**endsWith**

```
public boolean endsWith(String suffix)
```

**concat**

```
public String concat (String str)
```

**replace**

```
public String replace (char oldChar, char newChar)

public String replace (CharSequence target, CharSequence replacement)
```

**contains**

```
public boolean contains (CharSequence s)
```

Вопрос 1.

Для чего служит класс StringBuffer? Чем он отличается от String?

Вопрос 2.

Мы имеем переменную String str = "I love my string". Какое значение вернет вызов метода str.length()?

Вопрос 3.

Для переменной из предыдущего вопроса, как нам узнать 5-й символ? Проверьте свое решение, написав программу, выводящую на консоль 5-й символ строки. (Пример программы, выводящей информацию на консоль см. в разделе Hello World)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://comaqa.gitbook.io/java-automation/yazyk-java.-osnovy/stroki.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
