what is the substr? in the systemverilog - system-verilog

Int fd;
String str;
fd = $fopen(path, "r");
Status= $fgets(str, fd);
cm = str.substr(0,1);
cm1= str.substr(0,0);
I want to know what is substr function? What is the purpose above that??

The substr function returns a new string that is a substring formed by characters in position i through j of str. Very similar to examples posted here.
module test;
string str = "Test";
initial
$display(str.substr(0,1));
endmodule
The output will be:
>> Te
As you can see in section 6.16.8, IEEE SystemVerilog Standard 1800-2012.

substr function, as it name suggests, subtracts, or takes a chunk from a bigger string, in systemverilog.
Example:
stri0 = "my_lago";
stri1 = stri0.substr(1,5);
$display("This will give stri1 = %s" , stri1);
....
OUTPUT :- This will give stri1 = y_lag

Substring: This method extracts strings. It needs the Position of the substring ( start index, length). It then returns a new string with the characters in that range.
C# program Substring
using System;
class Program
{
static void Main()
{
string input = "ManCatDog";
// Get Middle three characters.
string subString = input.Substring(3, 6);
Console.WriteLine("SubString: {0}", subString);
}
}
Output
Substring: Cat

Related

Flutter: trim string after certain NUMBER of characters in dart

Say I have a string with n number of characters, but I want to trim it down to only 10 characters. (Given that at all times the string has greater that 10 characters)
I don't know the contents of the string.
How to trim it in such a way?
I know how to trim it after a CERTAIN character
String s = "one.two";
//Removes everything after first '.'
String result = s.substring(0, s.indexOf('.'));
print(result);
But how to remove it after a CERTAIN NUMBER of characters?
All answers (using substring) get the first 10 UTF-16 code units, which is different than the first 10 characters because some characters consist of two code units. It is better to use the characters package:
import 'package:characters/characters.dart';
void main() {
final str = "Hello 😀 World";
print(str.substring(0, 9)); // BAD
print(str.characters.take(9)); // GOOD
}
prints
➜ dart main.dart
Hello 😀
Hello 😀 W
With substring you might even get half a character (which isn't valid):
print(str.substring(0, 7)); // BAD
print(str.characters.take(7)); // GOOD
prints:
Hello �
Hello 😀
The above examples will fail if string's length is less than the trimmed length. The below code will work with both short and long strings:
import 'dart:math';
void main() {
String s1 = 'abcdefghijklmnop';
String s2 = 'abcdef';
var trimmed = s1.substring(0, min(s1.length,10));
print(trimmed);
trimmed = s2.substring(0, min(s2.length,10));
print(trimmed);
}
NOTE:
Dart string routines operate on UTF-16 code units. For most of Latin and Cyrylic languages that is not a problem since all characters will fit into a single code unit. Yet emojis, some Asian, African and Middle-east languages might need 2 code units to encode a single character. E.g. '😊'.length will return 2 although it is a single character string. See characters package.
I think this should work.
String result = s.substring(0, 10);
To trim a String to a certain number of characters. The. code below works perfectly well:
// initialise your string here
String s = 'one.two.three.four.five';
// trim the string by getting the first 10 characters
String trimmedString = s.substring(0, 10);
// print the first ten characters of the string
print(trimmedString);
Output:
one.two.th
i hope this helps
You can do this in multiple ways.
'string'.substr(start, ?length) USE :- 'one.two.three.four.five'.substr(0, 10)
'string'.substring(start, ?end) USE :- 'one.two.three.four.five'.substring(0, 10)
'string'.slice(start, ?end) USE :- 'one.two.three.four.five'.slice(0, 10)
To trim all trailing/right characters by specified characters, use the method:
static String trimLastCharacter(String srcStr, String pattern) {
if (srcStr.length > 0) {
if (srcStr.endsWith(pattern)) {
final v = srcStr.substring(0, srcStr.length - 1 - pattern.length);
return trimLastCharacter(v, pattern);
}
return srcStr;
}
return srcStr;
}
For example, you want to remove all 0 behind the decimals
$123.98760000
then, call it by
trimLastCharacter("$123.98760000", "0")
output:
$123.9876

Swift-How to write a variable or a value that is changing between parenthesis

(in swift language) For example " A + D " I want the string A to stay all the time but the value of D changes depending on let's say Hp, so when Hp is fd the string will be "A + fd" and etc
I mean like( "A + %s" % Hp ) for the string in python. Such as here: What does %s mean in Python?
If you are talking about %s, then it's a c-style formatting key, which awaits string variable or value in the list of arguments. In Swift, you compose strings using "\(variable)" syntax, which is called String interpolation, as explained in the documentation:
String Interpolation
String interpolation is a way to construct a new String value from a
mix of constants, variables, literals, and expressions by including
their values inside a string literal. You can use string interpolation
in both single-line and multiline string literals. Each item that you
insert into the string literal is wrapped in a pair of parentheses,
prefixed by a backslash ():
Source: official documentation
Example:
var myVar = "World"
var string = "Hello \(myVar)"
With non-strings:
let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// Output: message is "3 times 2.5 is 7.5"

Trouble padding a Perl string array without increasing array length

I have an integer value my $reading = 1200;.
I have an array my #DigitField = "000000000";
I want to replace the right-hand 4 elements of the array with $reading's value, and I want to do this programmatically using Perl's length function as shown below.
I've tried.
my #DigitField = "000000000";
my $reading = 1200;
splice #DigitField, length(#DigitField) + 1, length $reading, $reading;
print #DigitField;
but I'm getting
0000000001200
and I want the string to remain nine characters wide.
What are some other ways to replace part of a Perl string array?
I think you are possibly confused - the # sigil indicates #DigitField is an array variable. A string is not an array.
I think you want to format the number:
my $reading = 1200;
my $digitfield = sprintf('%09d', $reading);
print $digitfield, "\n";
I added a \n to the end of the print, this adds a newline. Depending on the context of your program, you may or may not want this in the final.

Please explain this specific line in my Java Code?

I have just started learning Java and I reached till arrays , I was preparing this program(From a book) on replacing space ' ' with '.' (dots) and i am not able to understand this specific line (its not mentioned even in the book I am learning from).
Please help me out.
class SpaceRemover{
public static void main(String[] args){
String mostFamous = "Hey there stackoverFLow ";
char [] mf1 = mostFamous.toCharArray();
for(int dex = 0; dex<mf1.length;dex++)
{
char current = mf1[dex]; // What is happening in this line ??
if (current != ' ') {
System.out.print(current);
}
else{
System.out.print('.');
}
}
System.out.println();
}
}
Someone please explain what is happening in "char current = mf1[dex];"
Thanks a lot for your time.
You are getting the dexth character/item within the character array mf1 (hence mf1[dex]) and storing it into the local variable current.
Basically a String in java is an array of characters. So what the above code does is converts the string to an array of chars so that it can access each index of the array later on. Then the code enters into a for loop in order to iterate through all the indecies of the char array.
Assuming that that is already clear to you, the code now creates a char variable which holds the current index of the array.
char current = mf1[dex];
mf1 is your char array that represents the string. dex is the current index of the char that is determined by the for loop. So by doing this we can check each character (letter) of the char array. Now if the char "current" is a blank space we can replace it with a dot.
It's getting the character at index idx in the array mf1 and storing its value in the current variable.
The for-loop is iterating the string mostFamous character by character.
the line you are asking is to get the character at specific position. Function is similar to JavaScript's charAt(i)
char current = mf1[dex];
This line gets values from the mf1 char array and assign to the current variable according to the dex, dex works as index to the array element and it increments with the running loop.
The line
char current = mf1[dex];
is placed inside a for loop where the variable dex is incremented each time the loop is iterated. The variable dex is the zero-based index of the array. On the left hand side of the assignment operator (=), you are declaring a variable named current of type char. On the right hand side of the assignment operator you are accessing the dex-th character of your CharArray, if you start counting from zero. The assignment operator binds the variable you declared with the value of the character you specified on the right hand side.
For example, the first time the loop is run, dex would start at 0, hence mf1[dex] (or mf1[0]) is just 'H'.
Here is solution
class SpaceRemover{
public static void main(String[] args){
String mostFamous = "Hey there stackoverFLow ";
char [] mf1 = mostFamous.toCharArray();
// mf1 contains={'H', 'e','y',' ','t','h',.........}
for(char current: mf1)
{
//the for-each loop assigns the value of mf1 variable to the current variable
//At first time the 'H' is assigned to the current and so-on
System.out.print(current==' '?'.':current );
}
System.out.println();
}
}
}
It assigns the element of the char array mf1 at the index dex to the char variable current.
Note that the for loop and that line may be simplified by using the foreach syntax; these two code blocks are equivalent:
// Your code
for(int dex = 0; dex<mf1.length;dex++) {
char current = mf1[dex];
// Equivalent code
for (char current : mf1) {
But further, the whole method may be replaced by one line:
public static void main(String[] args){
System.out.println("Hey there stackoverFLow ".replace(" ", "."));
}
char current = mf1[dex];
this will return the char element in char array whose index is dex
This is quite a basic usage of array.
Good luck with your study.
After this statement, char [] mf1 = mostFamous.toCharArray();
mf1[0]=H, mf1[1]=e, mf1[1]=y...
so at this line, char current = mf1[dex];
so, in first iteration, current=H, second iteration current=e...

How can I rearrange the chars in a char*?

I have char* myChar = "HELLO". I would like to switch the places of the E and the O. I tried doing myChar[1] = myChar[4], but that doesn't work. Please help!
First off, that string literal is probably being stored in read-only memory. You can fix that by declaring the string as an array of characters:
char myChar[] = "HELLO";
To swap the characters, you'll have to use a temporary variable:
char c1 = myChar[1];
myChar[1] = myChar[4];
myChar[4] = c1;
You assigned whatever is in myChar[4] into myChar[1]. (that's all you did there)
You need to create a temporary variable char temp; and do the following:
Edit: As mentioned by Tim Cooper, char myChar[] = "HELLO"; - // This will remove it's constness.
temp = myChar[1];
myChar[1] = myChar[4];
myChar[4] = temp;
This is a very common 'algorithm' to swap two things.