1. Introduction to StringObservable
1.介绍StringObservable
Working with String sequences in RxJava may be challenging; luckily RxJavaString provides us with all necessary utilities.
在RxJava中处理String序列可能具有挑战性;幸运的是RxJavaString为我们提供了所有必要的实用工具。
In this article, we’ll cover StringObservable which contains some helpful String operators. Therefore, before getting started, it’s advised to have a look at the Introduction to RxJava first.
在这篇文章中,我们将介绍StringObservable,它包含一些有用的String操作符。因此,在开始之前,建议先看看Introduction to RxJava先。
2. Maven Setup
2.Maven的设置
To get started, let’s include RxJavaString amongst our dependencies:
为了开始工作,让我们把RxJavaString包括在我们的依赖项中。
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava-string</artifactId>
<version>1.1.1</version>
</dependency>
The latest version of rxjava-string is available over on Maven Central.
最新版本的rxjava-string可以在Maven Central上找到。
3. StringObservable
3.StringObservable
StringObservable is a handy operator for representing a potentially infinite sequence of encoded Strings.
StringObservable是一个方便的操作符,用于表示可能是无限的编码字符串序列。
The operator from reads an input stream creating an Observable which emits character-bounded sequences of byte arrays:
操作符from读取一个输入流,创建一个Observable,该操作符发出有字符边界的字节数组序列。
We can create an Observable straight from an InputStream using a from operator:
我们可以使用from操作符从InputStream中直接创建Observable。
TestSubscriber testSubscriber = new TestSubscriber();
ByteArrayInputStream is = new ByteArrayInputStream("Lorem ipsum loream, Lorem ipsum lore".getBytes());
Observable<byte[]> observableByteStream = StringObservable.from(is);
// emits 8 byte array items
observableByteStream.subscribe(testSubscriber);
4. Converting Bytes into Strings
4.将字节转换为字符串
Encoding/decoding infinite sequences from different charsets can be done using decode and encode operators.
可以使用decode和encode操作器对不同字符集的无限序列进行编码/解码。
As their name may suggest, these will simply create an Observable that emits an encoded or decoded sequence of byte arrays or Strings, therefore, we could use it if we need to handle Strings in different charsets:
正如它们的名字所暗示的那样,这些将简单地创建一个Observable,发出一个编码或解码的字节数组或字符串序列,因此,如果我们需要处理不同字符集的字符串,我们可以使用它。
Decoding a byte array Observable:
解码一个字节数组Observable。
TestSubscriber testSubscriber = new TestSubscriber();
ByteArrayInputStream is = new ByteArrayInputStream(
"Lorem ipsum loream, Lorem ipsum lore".getBytes());
Observable<byte[]> byteArrayObservable = StringObservable.from(is);
Observable<String> stringObservable = StringObservable
.decode(byteArrayObservable, StandardCharsets.UTF_8);
// emits UTF-8 decoded strings,"Lorem ipsum loream, Lorem ipsum lore"
stringObservable.subscribe(testSubscriber);
5. Splitting Strings
5.分割字符串
StringObservable has also some convenient operators for splitting String sequences: split and byLine, both create a new Observable which chunks input data outputting items following a pattern:
StringObservable也有一些方便的操作符来分割String序列。split和byLine,都创建了一个新的Observable,它将输入数据分成几块,按照一个模式输出项目。
TestSubscriber testSubscriber = new TestSubscriber();
Observable<String> sourceObservable = Observable.just("Lorem ipsum loream,Lorem ipsum ", "lore");
Observable<String> splittedObservable = StringObservable.split(sourceObservable, ",");
// emits 2 strings "Lorem ipsum loream", "Lorem ipsum lore"
splittedObservable.subscribe(testSubscriber);
6. Joining Strings
6.连接弦乐
Complementary to previous section’s operators are join and stringConcat which concatenate items from a String Observable emitting a single string given a separator.
与上一节的操作符互补的是join和stringConcat,它们将String Observable中的项目连接起来,发出一个给定分隔符的单一字符串。
Also, note that these will consume all items before emitting an output.
另外,请注意,这些将在发出输出前消耗所有项目。
TestSubscriber testSubscriber = new TestSubscriber();
Observable<String> sourceObservable = Observable.just("Lorem ipsum loream", "Lorem ipsum lore");
Observable<String> joinedObservable = StringObservable.join(sourceObservable, ",");
// emits single string "Lorem ipsum loream,Lorem ipsum lore"
joinedObservable.subscribe(testSubscriber);
7. Conclusion
7.结论
This brief introduction to StringObservable demonstrated a few use cases of String manipulation using RxJavaString.
这篇关于StringObservable的简要介绍展示了使用RxJavaString.进行String操作的几个用例。
Examples in this tutorial and other examples on how to use StringObservable operators can be found over on GitHub.
本教程中的例子以及关于如何使用StringObservable操作符的其他例子可以在GitHub上找到over 。