我正在尝试用空格填充右边的字符串,以使不同的字符串长度在同一点对齐。为了做到这一点,我尝试使用以下内容。但是,我无法想象如何在%s内部使用变量namePad,就像我们使用实数一样(例如使用%(namePad)而不是%5s)
import UIKit
class NavigationController: UINavigationController {
override func viewDidLayoutSubviews() {
navigationBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100)
}
}
class ViewController: UIViewController {
override var prefersStatusBarHidden: Bool {
return true
}
}
输出样本:
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="NJV-2W-zhz">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Main-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="stackoverflow_43295886" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
<navigationItem key="navigationItem" title="Main" id="CEB-N4-YPg"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="897" y="791"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="rqG-Xx-QcO">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="NJV-2W-zhz" customClass="NavigationController" customModule="stackoverflow_43295886" customModuleProvider="target" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="klc-WP-4es">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="vdV-Sr-o02"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Dng-hb-kZ9" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="133.59999999999999" y="791.15442278860576"/>
</scene>
</scenes>
</document>
答案 0 :(得分:0)
您可以使用"%-"+namePad+"s"
代替"%-5s"
虽然,我不确定你真的需要那个变量。您正在使用String.format
,但没有给格式化变量,所以请尝试使用它。
String.format("%d - %-21s", id, name);
或者,和以前一样
int pad = 21;
return String.format("%d - %-"+pad+"s*", id, name);
例如,(添加*
以显示填充)
static class Person {
int id;
String name;
public Person(int id, String name) {
this.id = id; this.name = name;
}
public String toString() {
return String.format("%d - %-21s*", id, name);
}
}
public static void main (String[] args) throws java.lang.Exception
{
Person[] people = {
new Person(1, "Bill Gates"),
new Person(2, "Trump"),
new Person(3, "Tail"),
new Person(4, "James Bond")
};
for (Person p : people) {
System.out.println(p);
}
}
输出
1 - Bill Gates *
2 - Trump *
3 - Tail *
4 - James Bond *