MongoDBで文字列から明確な最初の単語を取得しますか?
文字列から明確な最初の単語を取得するには、distinct()を使用できます。まず、ドキュメントを使用してコレクションを作成しましょう-
> db.distinctFirstWordDemo.insertOne(
{
"_id": 100,
"StudentName":"John",
"StudentFeature": "John is a good player",
"Subject":"MongoDB"
}
);
{ "acknowledged" : true, "insertedId" : 100 }
> db.distinctFirstWordDemo.insertOne(
{
"_id": 101,
"StudentName":"Carol",
"StudentFeature": "Carol is not a good player",
"Subject":"MongoDB"
}
);
{ "acknowledged" : true, "insertedId" : 101 } find()メソッドを使用してコレクションからすべてのドキュメントを表示する-
> db.distinctFirstWordDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : 100,
"StudentName" : "John",
"StudentFeature" : "John is a good player",
"Subject" : "MongoDB"
}
{
"_id" : 101,
"StudentName" : "Carol",
"StudentFeature" : "Carol is not a good player",
"Subject" : "MongoDB"
} 以下は、文字列から明確な最初の単語を取得するためのクエリです-
> student = db.distinctFirstWordDemo.distinct("StudentFeature", {"Subject" : "MongoDB"}).map(function(st){
return st.split(" ")[0];
});
[ "John", "Carol" ]
> printjson(student); これにより、次の出力が生成されます-
[ "John", "Carol" ]
-
C++の文字列からの最初のX母音
この問題では、サイズNと整数Xの文字列str[]が与えられます。私たちのタスクは文字列から最初のX母音を出力するプログラムを作成することです 。 文字列から最初のX母音を印刷し、X母音未満が存在する場合は、-1を印刷します。 問題を理解するために例を見てみましょう Input: str = "learn C programming language", X = 5 Output: e, a, o, a, i Vowels are a, e, i, o, u ソリューションアプローチ この問題の簡単な解決策は、文字列文字をチャーターでトラバースすることです。そして、文
-
C#のすべての文字列から最初の3文字を取得します
以下はリスト内の文字列です- List<object> list = new List<object> { "keyboard", "mouse", "joystick", "monitor" }; 最初の3文字を使用するには、部分文字列メソッドを使用し、LinqSelectメソッドで使用します。 IEnumerable<string> res = list.AsQueryable() .Cast<string>() .Select(str => str.Su