I have been searching a lot online for a solution to this, but here is my question.
Basically I need to reverse a string of 4 characters: ABCD becomes DCBA.
Here is the start of the program:
import javax.swing.JOptionPane;
String input;
input = JOptionPane.showInputDialog("Enter a string with 4 characters (e.g. ABCD): " );
Thanks
The same way you do it in Java.
String output = new StringBuilder(input).reverse().toString();
You could also use a for loop to loop over the characters and build the String yourself.
Related
I want to write multiline string, with two lines, which will be equally indentated by 8 spaces. Ideally without need to have those 8 spaces in input, but I'm willing to do that. Anything so that I can have the result. I think i tried whole documentation. ', | > >-, >+, >8 ..., ... Adding 8char indentation which isn't in source is optional extra, but so far it seems, that in yaml you can have anything but what you actually typed.
• What is the combination for actual as-is multiline string without any yaml transformations (or say impossible if it's impossible)?
• What is the combination for actual as-is multiline string uniformly shifted to the right by N spaces (or say impossible if it's impossible)?
EDIT: specific example:
...
something: |
#blahblah
#blahblah
...
I want field somehting to contain 2 line string, each containing #blahblah, each prefixed by 8 spaces. In JSON you would write(I know I can do that in yaml, but I'd like to use yaml way to write yaml file):
{"something": " #blahblah\n #blahblah"}
EDIT 2: providing minimal working example.
Java code:
import java.io.InputStream;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;
public class Test {
public static void main(String[] args) {
Yaml yaml = new Yaml();
InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("test.yaml");
Map<String, Object> obj = yaml.load(inputStream);
String s = (String) obj.get("a");
System.out.println(s);
}
}
for input:
a: |
#abc
#def
I will get:
#abc
#def
For input:
a: |2
#abc
#def
I will get:
#abc
#def
How can this be explained??? If positive number after | should be for removing extra indentation, I understand it, that without number it will remove all indentation. So if someone want preserved indentation of 4 spaces, it seems he needs to do some math, put indentation of 5 and request to remove 1 using |1. No? This is how it should work? What am I missing?
What is the combination for actual as-is multiline string without any yaml transformations (or say impossible if it's impossible)?
--- |2
droggel
jug
You'll get the string as-is, without the 2 spaces indentation specified by the indentation indicator. Since the indicator must be at least 1 (you can't give 0), there is no way to do that without indentation. Still, this seems to do what you want.
What is the combination for actual as-is multiline string uniformly shifted to the right by N spaces (or say impossible if it's impossible)?
YAML is not a programming language and can't do any kind of string transformation, so this is impossible.
I have specific questions for my project
input = "3d6"
I want to convert this string some parts to integer. For instance I want to use input[0] like integer.
How can I do this?
There's two problems here:
How to convert a string to an integer
The most straightforward method is the Atoi (ASCII to integer) function in the strconv package., which will take a string of numeric characters and coerce them into an integer for you.
How to extract meaningful components of a known string pattern
In order to use strconv.Atoi, we need the numeric characters of the input by themselves. There's lots of ways to slice and dice a string.
You can just grab the first and last characters directly - input[:1] and input[2:] are the ticket.
You could split the string into two strings on the character "d". Look at the split method, a member of the strings package.
For more complex problems in this space, regular expressions are used. They're a way to define a pattern the computer can look for. For example, the regular expression ^x(\d+)$ will match on any string that starts with the character x and is followed by one or more numeric characters. It will provide direct access to the numeric characters it found by themselves.
Go has first class support for regular expressions via its regexp package.
For example,
package main
import (
"fmt"
)
func main() {
input := "3d6"
i := int(input[0] - '0')
fmt.Println(i)
}
Playground: https://play.golang.org/p/061miKcXdIF
Output:
3
is there any way or method to generate fake string using laravel faker ?
like in laravel we generate string upto 20 chars..
str_random(20);
Faker offers a couple of methods that let you replace placeholders in a given string with random characters:
lexify - takes given string and replaces ? with random letters
asciify - takes given string and replaces * with random ascii characters
numerify - takes given string and replaces # with random digits
bothify - combines the lexify and numerify
You could try to use one of them, depending on the requirements you have for that random string you need. asciify uses the largest set of characters as replacement so using that one makes most sense.
The following will give you a random string of 20 ascii characters:
$faker->asciify('********************')
Alternate for generate string without special chars.
$faker->regexify('[A-Za-z0-9]{20}')
$faker->text($maxNbChars = 50);
$faker->text()
// generates 50 char by default: "Aut quo omnis placeat eos omnis eos."
$faker->text(10);
// generates 10 char by default: "Labore."
All texts seems to be one or more latin pseudo-sentences with spaces and always a dot in the end (of each sentence).
uze Faker\Provider\en_US\Text
<?php
realText($maxNbChars = 200, $indexSize = 2) // "And yet I wish you could manage it?) 'And what are they made of?' Alice asked in a shrill, passionate voice. 'Would YOU like cats if you were never even spoke to Time!' 'Perhaps not,' Alice replied."
I am wondering how to make something where if X=5 and Y=2, then have it output something like
Hello 2 World 5.
In Java I would do
String a = "Hello " + Y + " World " + X;
System.out.println(a);
So how would I do that in TI-BASIC?
You have two issues to work out, concatenating strings and converting integers to a string representation.
String concatenation is very straightforward and utilizes the + operator. In your example:
"Hello " + "World"
Will yield the string "Hello World'.
Converting numbers to strings is not as easy in TI-BASIC, but a method for doing so compatible with the TI-83+/84+ series is available here. The following code and explanation are quoted from the linked page:
:"?
:For(X,1,1+log(N
:sub("0123456789",ipart(10fpart(N10^(-X)))+1,1)+Ans
:End
:sub(Ans,1,length(Ans)-1?Str1
With our number stored in N, we loop through each digit of N and store
the numeric character to our string that is at the matching position
in our substring. You access the individual digit in the number by
using iPart(10fPart(A/10^(X, and then locate where it is in the string
"0123456789". The reason you need to add 1 is so that it works with
the 0 digit.
In order to construct a string with all of the digits of the number, we first create a dummy string. This is what the "? is used
for. Each time through the For( loop, we concatenate the string from
before (which is still stored in the Ans variable) to the next numeric
character that is found in N. Using Ans allows us to not have to use
another string variable, since Ans can act like a string and it gets
updated accordingly, and Ans is also faster than a string variable.
By the time we are done with the For( loop, all of our numeric characters are put together in Ans. However, because we stored a dummy
character to the string initially, we now need to remove it, which we
do by getting the substring from the first character to the second to
last character of the string. Finally, we store the string to a more
permanent variable (in this case, Str1) for future use.
Once converted to a string, you can simply use the + operator to concatenate your string literals with the converted number strings.
You should also take a look at a similar Stack Overflow question which addresses a similar issue.
For this issue you can use the toString( function which was introduced in version 5.2.0. This function translates a number to a string which you can use to display numbers and strings together easily. It would end up like this:
Disp "Hello "+toString(Y)+" World "+toString(X)
If you know the length of "Hello" and "World," then you can simply use Output() because Disp creates a new line after every statement.
I am trying to split a string at the character ":" but cant create two separate strings from the split. If somebody could help me, I would appreciate it.
In RealBasic, the Split method doesn't create two (or more) separate strings but rather a single string array.
Dim s() As String = Split("Zero:One:Two", ":")
's() now contains the substrings like so:
's(0) = "Zero"
's(1) = "One"
's(2) = "Two"
Actually, the code is incorrect. It should be:
Dim s() As String = Split("Zero:One:Two", ":")
If you don't pass in the delimiter it assumes a space which wouldn't work in this case.
The online docs are at http://docs.realsoftware.com/index.php/Split
Split is best for actually splitting the text, but you can also use the string-manipulation methods: Left, Right, Mid and InStr.