I need your help to code the way to get the value that comes after 'code=' in these 3 examples.
var url='https://example.com.ar/buy?code=76''
var url2='https://example.com.ar/buy?v=1.1.1&code=100&box=4''
var url3='https://www.example.com/api/compra?box=1&code=1&id=60'.
I tried this option but it only works for one of the options
main() {
final String url='https://www.example.com/api/compra?box=1&code=76&id=60';
final int startIndex=url.indexOf('code=',0)+5;
final int endIndex=url.length;
final String substring=url.substring(startIndex, endIndex);
print (substring);
}
I look forward to your comments. Thank you very much!
final url='https://www.example.com/api/compra?box=1&code=76&id=60';
final code = Uri.parse(url).queryParameters['code'];
Related
Suppose I want to convert a UNIX integer into DateTime Format when called upon.
(That's just an example)
I want to create a function just like .toUpperCase(); , .toString(); where function takes that particular item as argument without writing inside brackets. I did try going inside these default core functions but didn't help.
How do I do that for custom functions?
right now I'm here:
//------------- main()----------//
void main() {
int unixDT = 1619691181;
// print(unixDT.toDateString()); //<-----this is what I'm looking for
}
//------- This is a function that should be in another-separate dart file-------//
String toDateString(int dt) {
DateTime datetime = new DateTime.fromMillisecondsSinceEpoch(dt);
return datetime.toString();
}
If you need clarification, please comment down. This is my very first question on StackOverflow so I apologize for mistakes if any.
Sounds like you are looking for extension methods:
void main() {
const unixDT = 1619691181;
print(unixDT.toDateString()); // 1970-01-19 18:54:51.181
}
extension IntToDateStringExtension on int {
String toDateString() => DateTime.fromMillisecondsSinceEpoch(this).toString();
}
You can use extension methods for this situations. You can add a method in Integer. For more details https://dart.dev/guides/language/extension-methods
You can try to use this to refer to same object that is calling that method, Hope that will help
You should use extension methods
Example:
extension ExtendedDynamic on dynamic {
double toDouble() {
final sValue = this?.toString() ?? '';
final double parsedValue = double?.tryParse(sValue) ?? 0.0;
return parsedValue;
}
}
void someMethod() {
final dynamic test = 0.0;
print(test.toDouble());
}
In your case it will be like this:
extension ExtendedInt on int {
String toDateString() {
final dateString = 'your code...';
return dateString;
}
}
void someMethod() {
int unixDT = 1619691181;
print(unixDT.toDateString());
}
Important thing : Wherever you want to use this extension, you must import it
import 'package:yourProject/extensions/int_extention.dart';
I am new to DART, and i would like to know is there any way to choose random characters from am given string in DART.
Any built-in functions available?
Thanks
Well, you can use the Random class from dart:math to make your own function:
import 'dart:math';
void main() {
print(getRandomLetter('Hello World'));
}
final _rnd = Random();
String getRandomLetter(String input) => input[_rnd.nextInt(input.length)];
I want to count total number of rows in a file.
Please explain your code if possible.
String fileAbsolutePath = "gs://sourav_bucket_dataflow/" + fileName;
PCollection<String> data = p.apply("Reading Data From File", TextIO.read().from(fileAbsolutePath));
PCollection<Long> count = data.apply(Count.<String>globally());
Now i want to get the value.
There are a variety of sinks that you can use to get data out of your pipeline. https://beam.apache.org/documentation/io/built-in/ has a list of the current built in IO transforms.
It sort of depends on what you want to do with that number. Assuming you want to use it in your future transformations, you may want to convert it to a PCollectionView object and pass it as a side input to other transformations.
PCollection<String> data = p.apply("Reading Data From File", TextIO.read().from(fileAbsolutePath));
PCollection<Long> count = data.apply(Count.<String>globally());
final PCollectionView<Long> view = count.apply(View.asSingleton());
A quick example to show you how to use the value as a side count:
data.apply(ParDo.of(new FuncFn(view)).withSideInputs(view));
Where:
class FuncFn extends DoFn<String,String>
{
private final PCollectionView<Long> mySideInput;
public FuncFn(PCollectionView<Long> mySideInput) {
this.mySideInput = mySideInput;
}
#ProcessElement
public void processElement(ProcessContext c) throws IOException
{
Long count = c.sideInput(mySideInput);
//other stuff you may want to do
}
}
Hope that helps!
where "input" in line 1 is the input. This will work.
PCollection<Long> number = input.apply(Count.globally());
number.apply(MapElements.via(new SimpleFunction<Long, Long>()
{
public Long apply(Long total)
{
System.out.println("Length is: " + total);
return total;
}
}));
I looking for the equivalent of
java.io.DataInputStream.readDouble() for Vala.
Is it even possible?
Currently I have :
public double data;
public override void load (DataInputStream dis) throws IOError {
data = dis.read_int64 ();
}
But it just converting a int64 to a double which is not what I want.
I've tried all sort of casting and de-referencing, but nothing seems to work.
This worked for me:
int main()
{
int64 foo = 0; // Whatever value you have
double data = *(double*)(&foo); // This is where the "magic" happens
stdout.printf("%f", data);
return 0;
}
Mind you, you may have to set the correct byte order for the conversion to succeed.
I am trying to wrap my head around Dart Streams. In particular this example of the command line utility cat has the following lines of code:
Stream<List<int>> stream = new File(path).openRead();
// Transform the stream using a `StreamTransformer`. The transformers
// used here convert the data to UTF8 and split string values into
// individual lines.
return stream
.transform(UTF8.decoder)
.transform(const LineSplitter())
.listen((line) {
if (showLineNumbers) {
stdout.write('${lineNumber++} ');
}
stdout.writeln(line);
}).asFuture().catchError((_) => _handleError(path));
The declaration of the Stream<T> as Stream<List<int>> has me a bit confused. Why is it not declared as a Stream<int>. How does the List<> type make this different. Are the subscriber events buffered in some way if it is a List?
What Type (as in <T>) is passed to the first transform? Is it an int or a List<int>?
What type is passed to each of the next transforms and what determines their type.
Does this example read the entire file before passing the results of the transform to the next transform? If so, is there an example somewhere of how to Stream very large files similar to this Node question Parsing huge logfiles in Node.js - read in line-by-line
Good question.
UTF8 is a Utf8Codec that extends Codec<String, List<int>>. So UTF8.decoder is a Converter<List<int>, String> that takes List<int> as parameter.
LineSplitter is a Converter<String, List<String>>. So it takes String as parameter. The resulting stream of .transform(const LineSplitter()) is a Stream<String> where each line is sent.
File.openRead doesn't read the entire file before writing the first bytes to the stream. So there's no problem to deal with large files.
Alexandre Ardhuin has the first three questions right. The 4th question however is not. After taking this apart and stubbing out my own version of the code I determined the following:
Even on a 37Mb file, the the transforms only get called once.
Here is the code I used to figure it out.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
void main(List<String> arguments) {
Stream<List<int>> stream = new File('Data.txt').openRead();
stream
.transform(const Utf8InterceptDecoder())
.transform(const LineSplitterIntercept())
.listen((line) {
// stdout.writeln(line);
}).asFuture().catchError((_) => print(_));
}
int lineSplitCount = 0;
class LineSplitterIntercept extends LineSplitter {
const LineSplitterIntercept() : super();
// Never gets called
List<String> convert(String data) {
stdout.writeln("LineSplitterIntercept.convert : Data:" + data);
return super.convert(data);
}
StringConversionSink startChunkedConversion(ChunkedConversionSink<String> sink) {
stdout.writeln("LineSplitterIntercept.startChunkedConversion Count:"+lineSplitCount.toString()+ " Sink: " + sink.toString());
lineSplitCount++;
return super.startChunkedConversion(sink);
}
}
int utfCount = 0;
class Utf8InterceptDecoder extends Utf8Decoder {
const Utf8InterceptDecoder() : super();
//never gets called
String convert(List<int> codeUnits) {
stdout.writeln("Utf8InterceptDecoder.convert : codeUnits.length:" + codeUnits.length.toString());
return super.convert(codeUnits);
}
ByteConversionSink startChunkedConversion(ChunkedConversionSink<String> sink) {
stdout.writeln("Utf8InterceptDecoder.startChunkedConversion Count:"+ utfCount.toString() + " Sink: "+ sink.toString());
utfCount++;
return super.startChunkedConversion(sink);
}
}